← DiffPush

Min Stack

Standard Bar Stack and Queues · Learning O(1) per operation · O(n)

The Vault's Lowest Alarm Gauge

A security vault stacks sealed boxes, and the control panel must flash the smallest serial number currently inside at any moment — without scanning the pile. Each panel action either drops a box, lifts the top box, reads the top serial, or flashes the minimum serial across the whole pile. Design the panel so every action answers instantly.

Input: A list of operations over one stack: ["push", value], ["pop"], ["top"], or ["getMin"].

Output: Return the list of values produced by every top and getMin operation, in order (push and pop produce no output).

Constraints

Examples

Example 1

Input: {"operations":[["push",-2],["push",0],["push",-3],["getMin"],["pop"],["top"],["getMin"]]}
Output: [-3,0,-2]
The pile bottoms out at -3; after lifting it, the top is 0 and the minimum rises to -2.

Example 2

Input: {"operations":[["push",1],["push",2],["top"],["getMin"],["pop"],["getMin"]]}
Output: [2,1,1]
Two pushes; the minimum never changes because 1 stays below 2 the whole time.

Solve this in your browser →

Also on LeetCode ↗