-
Notifications
You must be signed in to change notification settings - Fork 0
Stacks perform getmin in O(1) operation
Devrath edited this page Jul 27, 2021
·
3 revisions
PROBLEM STATEMENT: We are having a stack of integers, Now we need to have a function that returns the minimum value in the stack and this should hold in case of push and pop operations are done
- Assume there are 4 elements in stack
8-10-3-7. - Now if you want to get the minimum element now ----> Here the output is
3. - Now say you continuously pop 2 more times meaning, you remove
3and7, Here when you execute minimum element ---> Result is8. - Same way if you insert one new element(
1) now to remaining8-10, then the on executing minimum element ---> Result is1.
- Initial state on input in stack
8-10-3-7.