Skip to content

Practice Min Stack

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#155
LeetCode Problem

Min Stack

Problem Statement

Implement MinStack(), push(val), pop(), top(), and getMin(). push adds a value; pop removes the most recently added value; top reads it without removal. getMin reads the smallest currently stored value. Every operation must run in O(1) time. Calls that read or remove an element are made only when the stack is nonempty. Practice approach: Store each pushed value together with the minimum at its depth. Popping removes both pieces of information, restoring the previous minimum. Repeated minimum values are then handled naturally. Space is O(n).

Source: Min Stack on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
MinStack(); push(4); push(1); push(1); pop(); getMin(); top()
Output
[null,null,null,null,null,1,1]
Explanation

Removing one copy of the minimum must leave the other copy available.

Constraints

-(2^31) <= val <= 2^31 - 1
At most 30000 method calls.
pop, top and getMin are called on nonempty stacks.

Related LeetCode Problems

Practice more problems similar to Min Stack.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.