Preface
When I encounter an interview question, the question is roughly as follows:
Use two common stacks to implement a special stack, so that the pop, push, and min functions are all operations with the complexity of O (1). the min function is used to obtain the minimum value of the current stack.
Preliminary ideas
1. to implement the min function for the (1) operation, the first thought was to calculate the current minimum value in advance, so we would think of using a value to save the minimum element in the current stack, the value is maintained during the push and pop operations. In this way, min and push are all O (1), but pop is not. If the current pop-up is the minimum value, you need to find the minimum value of the current element. This is not o (1.
2. In addition, the above method does not use another stack, So I thought: store the sorted elements in one stack, and maintain this ordered stack in the push and pop operations,
In this case, the min operation is O (1), but the push and pop operations cannot think of the complexity of O (1) because they need to maintain this ordered stack.
At that time, I thought it was definitely to cache the minimum value information in another stack, but I don't know whether it was because I didn't eat or why, so my mind got stuck.
Correct Solution
If the problem cannot be solved, I feel very uncomfortable. So I began to think about how to make full use of the stack features and effectively cache the minimum value information for min operation.
The biggest feature of stack operations is that you can only operate the top elements of the stack. Think of the minimum value of each stack operation with an auxiliary stack cache, which is not just right. In this way, each pop operation will pop up on both sides. Because the top element of the secondary stack is the minimum value in the current stack, the push operation only needs to compare the elements of the inbound stack and the top elements of the secondary stack. In this way, push, pop, and min are all O (1) operations.
The text may not be clear. The code below is the PHP implementation, and the stack is simulated through arrays.
<? Php/*** use a secondary stack, O (1) complexity: calculates the minimum number in the stack * @ hack class and simulates the stack through arrays ** @ author laiwenhui */class strack {/*** data stack to store stack data; ** @ var array */private $ _ arrData = array ();/*** auxiliary stack, which stores the lowest value of each layer in the Data Group stack; ** @ var array */private $ _ arrMin = array ();/*** unit of the top stack ** @ var int */private $ _ top =-1; /*** output stack ** @ return bool | int */public function pop () {if ($ this-> _ top ===- 1) {return false ;} array_pop ($ this-> _ arrMin); $ thi S-> _ top --; return array_pop ($ this-> _ arrData );} /*** stack ** @ param int $ element * @ return bool */public function push ($ element) {$ element = intval ($ element ); // if the stack is empty, directly go to the stack if ($ this-> _ top ===- 1) {array_push ($ this-> _ arrData, $ element ); array_push ($ this-> _ arrMin, $ element); $ this-> _ top ++; return true ;}// not empty, determine whether the value of the inbound stack is smaller than the minimum stack top $ min = $ this-> _ arrMin [$ this-> _ top]; // compare and obtain the minimum value $ currentMin = $ element <$ min? $ Element: $ min; // array_push ($ this-> _ arrMin, $ currentMin) of the minimum value in the current stack ); // array_push ($ this-> _ arrData, $ element); $ this-> _ top ++; return true ;} /*** calculate the minimum value of the current stack space * @ return bool | int */public function min () {if ($ this-> _ top =-1) {return false;} return $ this-> _ arrMin [$ this-> _ top];}
Use:
Copy codeThe Code is as follows:
$ Obj = new strack ();
$ Obj-> push (12 );
$ Obj-> push (56 );
$ Obj-> push (23 );
$ Obj-> push (89 );
$ Obj-> push (4 );
Var_dump ($ obj-> min ());
$ Obj-> pop ();
Var_dump ($ obj-> min ());
$ Obj-> push (8 );
Var_dump ($ obj-> min ());
Output:
Copy codeThe Code is as follows:
Int (4)
Int (12)
Int (8)
OK to meet the requirements.
Do you have other better implementation methods? If yes, please let me know ^_^