Design a data structure that supports push, pop, and retrieveMin operations. The push operation adds an element to the top of the stack, the pop operation removes the top element from the stack, and the retrieveMin operation returns the minimum element currently in the stack.
Explanation: After pushing 10, 20, and 30, the minimum is 10. After popping twice, the stack has only 30, so the minimum is 30. After pushing 15 and 25, the minimum is 15.
Explanation: After pushing 50, the minimum is 50. After popping, the stack is empty, and the next push sets the new minimum. After pushing 40, the minimum is 40. After popping, the minimum becomes 30, then 20 after the next two pushes.
Stack with O(1) minimum retrieval
No dry run loaded.
🚀 Practice this problem
Run code, get AI hints & track streak