-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path00155-min_stack.java
50 lines (38 loc) · 978 Bytes
/
00155-min_stack.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// 155: Min stack
// https://leetcode.com/problems/min-stack/
import java.util.Stack;
class Solution {
// SOLUTION
static class MinStack {
Stack<Integer> s1 = new Stack<>();
Stack<Integer> s2 = new Stack<>();
public void push(int x) {
s1.push(x);
if (s2.isEmpty() || x <= getMin())
s2.push(x);
}
public void pop() {
if (s1.peek() == getMin())
s2.pop();
s1.pop();
}
public int top() {
return s1.peek();
}
public int getMin() {
return s2.peek();
}
}
public static void main(String[] args) {
MinStack o = new MinStack();
// OPERATIONS
o.push(-2);
o.push(0);
o.push(-3);
System.out.print(o.getMin() + " ");
o.pop();
o.top();
System.out.print(o.getMin() + " ");
System.out.println();
}
}