This is one of an interview question. You need to design a stack which holds an integer value such that getMinimum() function should return the minimum element in the stack.
For example: consider the below example
case #1 5 --> TOP 1 4 6 2 When getMinimum() is called it should return 1, which is the minimum element in the stack. case #2 stack.pop() stack.pop() Note: Both 5 and 1 are poped out of the stack. So after this, the stack looks like, 4 --> TOP 6 2 When getMinimum() is called is should return 2 which is the minimum in the stack.
Constriants:
- getMinimum should return the minimum value in O(1)
- Space constraint also has to be considered while designing it and if you use extra space, it should be of constant space.
I think you can simply use a LinkedList in your stack implementation.
First time you push a value, you put this value as the linkedlist head.
then each time you push a value, if the new value < head.data, make a prepand operation ( which means the head becomes the new value )
if not, then make an append operation.
When you make a pop(), you check if min == linkedlist.head.data, if yes, then head=head.next;
Here is my code.
EDIT: This fails the "constant space" constraint - it basically doubles the space required. I very much doubt that there's a solution which doesn't do that though, without wrecking the runtime complexity somewhere (e.g. making push/pop O(n)). Note that this doesn't change the complexity of the space required, e.g. if you've got a stack with O(n) space requirements, this will still be O(n) just with a different constant factor.
Non-constant-space solution
Keep a "duplicate" stack of "minimum of all values lower in the stack". When you pop the main stack, pop the min stack too. When you push the main stack, push either the new element or the current min, whichever is lower.
getMinimum()
is then implemented as justminStack.peek()
.So using your example, we'd have:
After popping twice you get:
Please let me know if this isn't enough information. It's simple when you grok it, but it might take a bit of head-scratching at first :)
(The downside of course is that it doubles the space requirement. Execution time doesn't suffer significantly though - i.e. it's still the same complexity.)
EDIT: There's a variation which is slightly more fiddly, but has better space in general. We still have the min stack, but we only pop from it when the value we pop from the main stack is equal to the one on the min stack. We only push to the min stack when the value being pushed onto the main stack is less than or equal to the current min value. This allows duplicate min values.
getMinimum()
is still just a peek operation. For example, taking the original version and pushing 1 again, we'd get:Popping from the above pops from both stacks because 1 == 1, leaving:
Popping again only pops from the main stack, because 5 > 1:
Popping again pops both stacks because 1 == 1:
This ends up with the same worst case space complexity (double the original stack) but much better space usage if we rarely get a "new minimum or equal".
EDIT: Here's an implementation of Pete's evil scheme. I haven't tested it thoroughly, but I think it's okay :)
I am posting the complete code here to find min and max in a given stack.
Time complexity will be O(1)..
Let me know if you are facing any issue
Thanks, Vikash
To getMin elements from Stack. We have to use Two stack .i.e Stack s1 and Stack s2.
---------------------Recursively call Step 2 to 4-----------------------
if New element added to stack s1.Then pop elements from stack s2
compare new elments with s2. which one is smaller , push to s2.
pop from stack s2(which contains min element)
Code looks like: