-
-
Notifications
You must be signed in to change notification settings - Fork 605
Expand file tree
/
Copy pathMinStack.java
More file actions
39 lines (30 loc) · 689 Bytes
/
MinStack.java
File metadata and controls
39 lines (30 loc) · 689 Bytes
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
package problems.easy;
import java.util.Stack;
/**
* Created by sherxon on 2016-12-29.
*/
public class MinStack {
private Stack<Integer> stack;
private Stack<Integer> min;
/** initialize your data structure here. */
public MinStack() {
stack = new Stack<>();
min = new Stack<>();
}
public void push(int x) {
stack.push(x);
if (min.isEmpty() || x <= min.peek())
min.push(x);
}
public void pop() {
int p = stack.pop();
if (min.peek() == p)
min.pop();
}
public int top() {
return stack.peek();
}
public int getMin() {
return min.peek();
}
}