-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_155_min_stack.java
More file actions
47 lines (41 loc) · 1.09 KB
/
Copy path_155_min_stack.java
File metadata and controls
47 lines (41 loc) · 1.09 KB
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
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
public class _155_min_stack {
public Deque<List<Integer>> stack = new ArrayDeque<>();
public _155_min_stack() {
}
public void push(int value) {
int currentMin = 0;
if(stack.isEmpty())currentMin=value;
else{
List<Integer> first = stack.peekFirst();
currentMin = first.get(1);
}
currentMin = Math.min(value, currentMin);
List<Integer> node = new ArrayList<>();
node.add(0, value);
node.add(1, currentMin);
stack.push(node);
}
public void pop() {
if(!stack.isEmpty()){
stack.pop();
}
}
public int top() {
if(stack.isEmpty())return 0;
else{
List<Integer> first = stack.peek();
return first.get(0);
}
}
public int getMin() {
if(stack.isEmpty())return 0;
else{
List<Integer> first = stack.peekFirst();
return first.get(1);
}
}
}