Skip to content

Latest commit

 

History

History
62 lines (46 loc) · 1.22 KB

_155. Min Stack.md

File metadata and controls

62 lines (46 loc) · 1.22 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 16, 2024

Last updated : July 01, 2024


Related Topics : Stack, Design

Acceptance Rate : 55.24 %


Solutions

Python

# Prompt: All operations must be O(1)

class MinStack {
    private Stack<int[]> vals;
    public MinStack() {
        this.vals = new Stack<>();
    }
    
    public void push(int val) {
        int newMin = (vals.size() > 0 && vals.peek()[1] < val) ? vals.peek()[1] : val;
        vals.push(new int[] {val, newMin});
    }
    
    public void pop() {
        vals.pop();
    }
    
    public int top() {
        return vals.peek()[0];
    }
    
    public int getMin() {
        return vals.peek()[1];
    }
}

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack obj = new MinStack();
 * obj.push(val);
 * obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.getMin();
 */