PIXELBANKv9.1.0
Menu

Design a stack that supports push, pop, top, and retrieving the minimum element in O(1) time.

Input: operations separated by semicolons. Output results of top, getMin operations.

Example:

Input:
push,-2;push,0;push,-3;getMin;pop;top;getMin
Output:
-3
0
-2
Reasoning:
  • We start with an empty stack and apply the operations in sequence: push -2, push 0, push -3.
  • The getMin operation returns the current minimum element, which is -3, so the first output is −3-3.
  • We then pop the top element (-3), and the top operation returns the new top element, which is 0.
  • After the top operation, another getMin operation is performed, returning the new minimum element, which is -2.

Constraints:

  • -2^31 <= val <= 2^31 - 1
  • pop, top, getMin always called on non-empty stack
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
Min Stack - Medium | PixelBank