BackhardStack

Dynamic Minimum Keeper Solution

Problem Statement

Design a stack that supports the standard push and pop operations, along with a minKeep operation which returns the smallest element in the stack. Implement the stack such that all operations are performed in O(1) time complexity.

Example 1
Input
push(97) -> push(48) -> push(35) -> minKeep() -> pop() -> minKeep()
Output
undefined

Explanation: After pushing 97, 48, and 35, the smallest element is 35. After popping one element, the smallest element becomes 48.

Example 2
Input
push(25) -> push(52) -> minKeep() -> push(12) -> minKeep()
Output
undefined

Explanation: Pushing 25 and 52, the smallest is 25. After pushing 12, the smallest element becomes 12.

Constraints

  • The stack will contain a maximum of 1000 elements
  • Each element will be in the range of 1 to 10^5
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