Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Runtime: 47 ms, faster than 72.11% of Java online submissions for Min Stack.
- // Memory Usage: 40.3 MB, less than 29.71% of Java online submissions for Min Stack.
- class MinStack {
- /** initialize your data structure here. */
- private Deque<Integer> values;
- private Deque<Integer> minStack;
- public MinStack() {
- values = new ArrayDeque<>();
- minStack = new ArrayDeque<>();
- }
- public void push(int x) {
- values.push(x);
- if (minStack.isEmpty()) {
- minStack.push(x);
- } else if (minStack.peek() > x) {
- minStack.push(x);
- } else {
- minStack.push(minStack.peek());
- }
- }
- public void pop() {
- int val = values.pop();
- int min = minStack.pop();
- }
- public int top() {
- return values.peek();
- }
- public int getMin() {
- return minStack.peek();
- }
- }
- /**
- * Your MinStack object will be instantiated and called as such:
- * MinStack obj = new MinStack();
- * obj.push(x);
- * obj.pop();
- * int param_3 = obj.top();
- * int param_4 = obj.getMin();
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement