A stack is required. In addition to the ordinary pop () and push () functions, there is also a max () function that returns the maximum value in the stack, time Complexity and space complexity are required as low as possible.
First, it is easy to think of the algorithm with the time complexity of O (1). We set an array of MAX [] and Max [I] to indicate the maximum value in the range from the bottom of the stack to the position I, then we insert is, set the current insert position is Po, then we compare MAX [po-1] and insert element x size, compared to the limit update MAX [po], then, when inserting X and POP (), it will pop up as usual. When calculating the getmax () function, we only need to return MAX [Top] (top is the top position of the stack ). The time complexity of this algorithm is O (1), and the space complexity is O (n ).
Obviously, it is easy to see that we do not need to maintain the maximum value from all locations to the bottom of the stack. It is easy to find that Max needs to be updated only when the inserted value is larger than all elements in the current stack, therefore, we replace the max array with a linked list. The linked list refers to the position where the max value is updated one time. When inserting, compare the inserted element with the current maximum value. If the value is smaller than nothing, add a node as the current node. When POP () is used, check whether the position of the pop-up element is the position pointed to by the current node. If yes, update the current node to the next node (if no error code is returned). If the getmax () function is used, return the value at the position represented by the current node. In this case, the average time complexity is the same as the preceding algorithm, but the space complexity is greatly improved. However, in extreme cases, for example, if an element is inserted in ascending order, the space complexity is O (n ).
The following describes the algorithm of O (1) for both time complexity and space complexity. Apart from the original stack [] array, we only need to top two additional variables to indicate the position of the top element of the stack, max indicates the maximum value in the current stack. When we insert element x, we insert not X but Max-X. At this time, if Max-X is smaller than 0, it means that the inserted element is greater than the maximum value in the current stack, this is to update Max. In the pop-up, if the pop-up element x is greater than 0, it indicates that this element is smaller than the maximum value in the stack, Then we return max-X, (because we saved Max-X in the front, then Max-(Max-x) = X, it is easy to find the value of the village in the stack ), otherwise, the number to be popped up is the maximum value in the stack. After Max is popped up, we also need to update max = MAX + x (x <0 ), at this time, Max is still the maximum value in the current stack (after removing X) (think about why), and The getmax () function returns Max directly. In this way, we can complete the required functions in O (1) time complexity and O (1) space complexity. For detailed implementation, see the following code.
# Include <stdio. h> # define maxn 110 using namespace STD; int stack [maxn], top, max = 0; // Max is initialized to 0, you may wish to set the elements in the stack to be greater than 0 void push (int x) {stack [top ++] = max-X; If (x> MAX) max = x ;} int POP () {If (Top = 0) // stack empty return-1; // error code int X; If (stack [Top-1] <0) // indicates that the number of outgoing Stacks is the maximum value in the current stack. In this case, update max {x = max; MAX + = stack [-- top];}. else {x = max-stack [-- top];} return X;} int getmax () {If (Top = 0) // empty return-1 stack; // error code return Max;} int main () {// freopen ("dd.txt", "r", stdin); While (1) {int A, B; scanf ("% d", & A); if (a = 1) // execute the push (x) Statement {scanf ("% d", & B ); push (B);} else if (a = 2) // execute the pop statement printf ("% d \ n", pop ()); else if (a = 3) // execute the getmax () Statement printf ("% d \ n", getmax (); else // exit break;} return 0 ;}