Site icon Wander In Dev

Min Stack (LeetCode #155)

Challenge Statement

Constraints

Example 1:

Input
["MinStack","push","push","push","getMin","pop","top","getMin"]
[[], [-2], [0], [-3], [], [], [], []]

Output
[null, null, null, null, -3, null, 0, -2]

Explanation
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); // return -3
minStack.pop();
minStack.top();    // return 0
minStack.getMin(); // return -2

Solution

Below is my solution and some test cases. This solution has a constant time complexity O(1) and a constant space complexity O(1).

Exit mobile version