BackhardDesign

Efficient Minimum Value Tracker Solution

Problem Statement

Design a data structure that supports the standard stack operations (push, pop, top) and an additional operation to retrieve the minimum element in O(1) time. The data structure should be able to handle a large number of elements and operations efficiently.

Example 1
Input
push(17), push(29), push(7), getMin(), pop(), getMin()
Output
undefined

Explanation: The minimum value after the first three push operations is 7. After popping one element, the minimum value becomes 17.

Example 2
Input
push(23), push(13), push(25), pop(), getMin()
Output
undefined

Explanation: The minimum value after the first three push operations is 13. After popping one element, the minimum value becomes 23.

Constraints

  • The data structure will be initialized with an empty stack.
  • The data structure will support at most 10000 operations (push, pop, top, getMin).
  • All elements pushed onto the stack will be in the range [1, 10000].
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free