Stack with find-min/find-max in O(1) time
Problem
Stack with find-min/find-max in O(1) time
Solution
Method 1 - using extra stacks for min and max
The basic idea of the solution can be found here.
Here instead of 2 stacks we will be using 3 stacks -1 stack with normal elements, 1 with only max elements and 1 with min element.
Now the operations will be following :
push:
- If the element being pushed is less than the top element of ‘min_stack’ then push it on ‘min_stack’ as well.
[Read More]