-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path155_min_stack.java
More file actions
62 lines (52 loc) · 1.18 KB
/
155_min_stack.java
File metadata and controls
62 lines (52 loc) · 1.18 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* Solution: Linked list */
class Node {
int val;
int min;
Node next;
public Node(int val) {
this.val = val;
this.next = null;
this.min = val;
}
public void updateMin(int preMin) {
this.min = (preMin < this.min) ? preMin : this.min;
}
}
class MinStack {
private PriorityQueue<Integer> pq;
private Node end;
/** initialize your data structure here. */
public MinStack() {
end = null;
}
public void push(int x) {
Node node = new Node(x);
if (end == null) {
node.updateMin(Integer.MAX_VALUE);
end = node;
} else {
node.updateMin(end.min);
node.next = end;
end = node;
}
}
public void pop() {
if (end == null)
return;
end = end.next;
}
public int top() {
return end.val;
}
public int getMin() {
return end.min;
}
}
/**
* 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();
*/