Question: Design a Data Structure SpecialStack that supports all the stack operations like push(), pop(), isEmpty(), isFull() and an additional operation getMin() which should return minimum element from the SpecialStack. All these operations of SpecialStack must be O(1). To implement SpecialStack, you should only use standard Stack data structure and no other data structure like arrays, list, .. etc.

Example:

Consider the following SpecialStack
16  --> TOP
15
29
19
18

When getMin() is called it should return 15, which is the minimum 
element in the current stack. 

If we do pop two times on stack, the stack becomes
29  --> TOP
19
18

When getMin() is called, it should return 18 which is the minimum 
in the current stack.

Solution: Use two stacks: one to store actual stack elements and other as an auxiliary stack to store minimum values. The idea is to do push() and pop() operations in such a way that the top of auxiliary stack is always the minimum. Let us see how push() and pop() operations work.

[ad type=”banner”]

Push(int x) // inserts an element x to Special Stack
1) push x to the first stack (the stack with actual elements)
2) compare x with the top element of the second stack (the auxiliary stack). Let the top element be y.
…..a) If x is smaller than y then push x to the auxiliary stack.
…..b) If x is greater than y then push y to the auxiliary stack.

int Pop() // removes an element from Special Stack and return the removed element
1) pop the top element from the auxiliary stack.
2) pop the top element from the actual stack and return it.

The step 1 is necessary to make sure that the auxiliary stack is also updated for future operations.

int getMin() // returns the minimum element from Special Stack
1) Return the top element of auxiliary stack.

We can see that all above operations are O(1).
Let us see an example. Let us assume that both stacks are initially empty and 18, 19, 29, 15 and 16 are inserted to the SpecialStack.

When we insert 18, both stacks change to following.
Actual Stack
18 <--- top     
Auxiliary Stack
18 <---- top

When 19 is inserted, both stacks change to following.
Actual Stack
19 <--- top     
18
Auxiliary Stack
18 <---- top
18

When 29 is inserted, both stacks change to following.
Actual Stack
29 <--- top     
19
18
Auxiliary Stack
18 <---- top
18
18

When 15 is inserted, both stacks change to following.
Actual Stack
15 <--- top     
29
19 
18
Auxiliary Stack
15 <---- top
18
18
18

When 16 is inserted, both stacks change to following.
Actual Stack
16 <--- top     
15
29
19 
18
Auxiliary Stack
15 <---- top
15
18
18
18

Following is C++ implementation for SpecialStack class. In the below implementation, SpecialStack inherits from Stack and has one Stack object min which work as auxiliary stack.

C++ Programming:

[pastacode lang=”cpp” manual=”%23include%3Ciostream%3E%0A%23include%3Cstdlib.h%3E%0A%20%0Ausing%20namespace%20std%3B%0A%20%0A%2F*%20A%20simple%20stack%20class%20with%20basic%20stack%20funtionalities%20*%2F%0Aclass%20Stack%0A%7B%0Aprivate%3A%0A%20%20%20%20static%20const%20int%20max%20%3D%20100%3B%0A%20%20%20%20int%20arr%5Bmax%5D%3B%0A%20%20%20%20int%20top%3B%0Apublic%3A%0A%20%20%20%20Stack()%20%7B%20top%20%3D%20-1%3B%20%7D%0A%20%20%20%20bool%20isEmpty()%3B%0A%20%20%20%20bool%20isFull()%3B%0A%20%20%20%20int%20pop()%3B%0A%20%20%20%20void%20push(int%20x)%3B%0A%7D%3B%0A%20%0A%2F*%20Stack’s%20member%20method%20to%20check%20if%20the%20stack%20is%20iempty%20*%2F%0Abool%20Stack%3A%3AisEmpty()%0A%7B%0A%20%20%20%20if(top%20%3D%3D%20-1)%0A%20%20%20%20%20%20%20%20return%20true%3B%0A%20%20%20%20return%20false%3B%0A%7D%0A%20%0A%2F*%20Stack’s%20member%20method%20to%20check%20if%20the%20stack%20is%20full%20*%2F%0Abool%20Stack%3A%3AisFull()%0A%7B%0A%20%20%20%20if(top%20%3D%3D%20max%20-%201)%0A%20%20%20%20%20%20%20%20return%20true%3B%0A%20%20%20%20return%20false%3B%0A%7D%0A%20%0A%2F*%20Stack’s%20member%20method%20to%20remove%20an%20element%20from%20it%20*%2F%0Aint%20Stack%3A%3Apop()%0A%7B%0A%20%20%20%20if(isEmpty())%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20cout%3C%3C%22Stack%20Underflow%22%3B%0A%20%20%20%20%20%20%20%20abort()%3B%0A%20%20%20%20%7D%0A%20%20%20%20int%20x%20%3D%20arr%5Btop%5D%3B%0A%20%20%20%20top–%3B%0A%20%20%20%20return%20x%3B%0A%7D%0A%20%0A%2F*%20Stack’s%20member%20method%20to%20insert%20an%20element%20to%20it%20*%2F%0Avoid%20Stack%3A%3Apush(int%20x)%0A%7B%0A%20%20%20%20if(isFull())%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20cout%3C%3C%22Stack%20Overflow%22%3B%0A%20%20%20%20%20%20%20%20abort()%3B%0A%20%20%20%20%7D%0A%20%20%20%20top%2B%2B%3B%0A%20%20%20%20arr%5Btop%5D%20%3D%20x%3B%0A%7D%0A%20%0A%2F*%20A%20class%20that%20supports%20all%20the%20stack%20operations%20and%20one%20additional%0A%20%20operation%20getMin()%20that%20returns%20the%20minimum%20element%20from%20stack%20at%0A%20%20any%20time.%20%20This%20class%20inherits%20from%20the%20stack%20class%20and%20uses%20an%0A%20%20auxiliarry%20stack%20that%20holds%20minimum%20elements%20*%2F%0Aclass%20SpecialStack%3A%20public%20Stack%0A%7B%0A%20%20%20%20Stack%20min%3B%0Apublic%3A%0A%20%20%20%20int%20pop()%3B%0A%20%20%20%20void%20push(int%20x)%3B%0A%20%20%20%20int%20getMin()%3B%0A%7D%3B%0A%20%0A%2F*%20SpecialStack’s%20member%20method%20to%20insert%20an%20element%20to%20it.%20This%20method%0A%20%20%20makes%20sure%20that%20the%20min%20stack%20is%20also%20updated%20with%20appropriate%20minimum%0A%20%20%20values%20*%2F%0Avoid%20SpecialStack%3A%3Apush(int%20x)%0A%7B%0A%20%20%20%20if(isEmpty()%3D%3Dtrue)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20Stack%3A%3Apush(x)%3B%0A%20%20%20%20%20%20%20%20min.push(x)%3B%0A%20%20%20%20%7D%0A%20%20%20%20else%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20Stack%3A%3Apush(x)%3B%0A%20%20%20%20%20%20%20%20int%20y%20%3D%20min.pop()%3B%0A%20%20%20%20%20%20%20%20min.push(y)%3B%0A%20%20%20%20%20%20%20%20if(%20x%20%3C%20y%20)%0A%20%20%20%20%20%20%20%20%20%20min.push(x)%3B%0A%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20%20min.push(y)%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F*%20SpecialStack’s%20member%20method%20to%20remove%20an%20element%20from%20it.%20This%20method%0A%20%20%20removes%20top%20element%20from%20min%20stack%20also.%20*%2F%0Aint%20SpecialStack%3A%3Apop()%0A%7B%0A%20%20%20%20int%20x%20%3D%20Stack%3A%3Apop()%3B%0A%20%20%20%20min.pop()%3B%0A%20%20%20%20return%20x%3B%0A%7D%0A%20%0A%2F*%20SpecialStack’s%20member%20method%20to%20get%20minimum%20element%20from%20it.%20*%2F%0Aint%20SpecialStack%3A%3AgetMin()%0A%7B%0A%20%20%20%20int%20x%20%3D%20min.pop()%3B%0A%20%20%20%20min.push(x)%3B%0A%20%20%20%20return%20x%3B%0A%7D%0A%20%0A%2F*%20Driver%20program%20to%20test%20SpecialStack%20methods%20*%2F%0Aint%20main()%0A%7B%0A%20%20%20%20SpecialStack%20s%3B%0A%20%20%20%20s.push(10)%3B%0A%20%20%20%20s.push(20)%3B%0A%20%20%20%20s.push(30)%3B%0A%20%20%20%20cout%3C%3Cs.getMin()%3C%3Cendl%3B%0A%20%20%20%20s.push(5)%3B%0A%20%20%20%20cout%3C%3Cs.getMin()%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

Output:
10
5

[ad type=”banner”]

Space Optimized Version
The above approach can be optimized. We can limit the number of elements in auxiliary stack. We can push only when the incoming element of main stack is smaller than or equal to top of auxiliary stack. Similarly during pop, if the pop off element equal to top of auxiliary stack, remove the top element of auxiliary stack. Following is modified implementation of push() and pop().

 

[pastacode lang=”cpp” manual=”%2F*%20SpecialStack’s%20member%20method%20to%20insert%20an%20element%20to%20it.%20This%20method%0A%20%20%20makes%20sure%20that%20the%20min%20stack%20is%20also%20updated%20with%20appropriate%20minimum%0A%20%20%20values%20*%2F%0Avoid%20SpecialStack%3A%3Apush(int%20x)%0A%7B%0A%20%20%20%20if(isEmpty()%3D%3Dtrue)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20Stack%3A%3Apush(x)%3B%0A%20%20%20%20%20%20%20%20min.push(x)%3B%0A%20%20%20%20%7D%0A%20%20%20%20else%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20Stack%3A%3Apush(x)%3B%0A%20%20%20%20%20%20%20%20int%20y%20%3D%20min.pop()%3B%0A%20%20%20%20%20%20%20%20min.push(y)%3B%0A%20%20%0A%20%20%20%20%20%20%20%20%2F*%20push%20only%20when%20the%20incoming%20element%20of%20main%20stack%20is%20smaller%20%0A%20%20%20%20%20%20%20%20than%20or%20equal%20to%20top%20of%20auxiliary%20stack%20*%2F%0A%20%20%20%20%20%20%20%20if(%20x%20%3C%3D%20y%20)%0A%20%20%20%20%20%20%20%20%20%20min.push(x)%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F*%20SpecialStack’s%20member%20method%20to%20remove%20an%20element%20from%20it.%20This%20method%0A%20%20%20removes%20top%20element%20from%20min%20stack%20also.%20*%2F%0Aint%20SpecialStack%3A%3Apop()%0A%7B%0A%20%20%20%20int%20x%20%3D%20Stack%3A%3Apop()%3B%0A%20%20%20%20int%20y%20%3D%20min.pop()%3B%0A%20%0A%20%20%20%20%2F*%20Push%20the%20popped%20element%20y%20%20back%20only%20if%20it%20is%20not%20equal%20to%20x%20*%2F%0A%20%20%20%20if%20(%20y%20!%3D%20x%20)%0A%20%20%20%20%20%20min.push(y)%3B%0A%20%0A%20%20%20%20return%20x%3B%0A%7D” message=”” highlight=”” provider=”manual”/] [ad type=”banner”]

Categorized in: