BackhardStack

Efficient Minimal Value Retrieval Solution

Problem Statement

Design a stack that supports constant time complexity for retrieving the minimum element, along with standard push and pop operations.

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

Explanation: Push 17 and 23 onto the stack. The minimum value is 17. After popping one element (17), the minimum value becomes 23.

Example 2
Input
push(42), push(91), push(13), getMin(), pop(), getMin()
Output
undefined

Explanation: Push 42, 91, and 13 onto the stack. The minimum value is 13. After popping one element (13), the minimum value becomes 42.

Constraints

  • All elements pushed onto the stack are integers.
  • The stack does not support duplicate values for the minimum element.
  • At most 1000 operations (push, pop, getMin) will be performed.
  • Integer values pushed onto the stack are within the range of -2^31 to 2^31 - 1.
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