Skip to content

Commit 2c23741

Browse files
authored
Update Max Stack.java
1 parent 183aed5 commit 2c23741

File tree

1 file changed

+60
-22
lines changed

1 file changed

+60
-22
lines changed

Easy/Max Stack.java

Lines changed: 60 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,83 @@
11
class MaxStack {
22

3-
/** initialize your data structure here. */
4-
Stack<Integer> stack;
5-
Stack<Integer> max;
3+
private Node stackHead;
4+
private Node stackTail;
5+
private PriorityQueue<Integer> maxValues;
6+
private Map<Integer, Stack<Node>> valToNodeMapping;
7+
68
public MaxStack() {
7-
stack = new Stack<>();
8-
max = new Stack<>();
9+
this.stackHead = new Node(Integer.MIN_VALUE);
10+
this.stackTail = new Node(Integer.MAX_VALUE);
11+
this.stackTail.prev = this.stackHead;
12+
this.stackHead.next = this.stackTail;
13+
this.maxValues = new PriorityQueue<>((o1, o2) -> o2 - o1);
14+
this.valToNodeMapping = new HashMap<>();
915
}
1016

1117
public void push(int x) {
12-
stack.push(x);
13-
max.push(max.isEmpty() ? x : Math.max(x, max.peek()));
18+
Node node = new Node(x);
19+
if (!this.valToNodeMapping.containsKey(x)) {
20+
this.maxValues.add(x);
21+
}
22+
addNodeToStack(node);
23+
this.valToNodeMapping.computeIfAbsent(x, k -> new Stack<>()).push(node);
1424
}
1525

1626
public int pop() {
17-
max.pop();
18-
return stack.pop();
27+
Node toRemove = this.stackHead.next;
28+
this.valToNodeMapping.get(toRemove.val).pop();
29+
removeNodeFromStack(toRemove);
30+
return toRemove.val;
1931
}
2032

2133
public int top() {
22-
return stack.peek();
34+
return this.stackHead.next.val;
2335
}
2436

2537
public int peekMax() {
26-
return max.peek();
38+
moveToMaxValue();
39+
int maxVal = this.maxValues.peek();
40+
return this.valToNodeMapping.get(maxVal).peek().val;
2741
}
2842

2943
public int popMax() {
30-
int num = max.peek();
31-
Stack<Integer> temp = new Stack<>();
32-
while (stack.peek() != num) {
33-
temp.push(stack.pop());
34-
max.pop();
44+
moveToMaxValue();
45+
int maxVal = this.maxValues.peek();
46+
Node toRemove = this.valToNodeMapping.get(maxVal).pop();
47+
removeNodeFromStack(toRemove);
48+
return toRemove.val;
49+
}
50+
51+
private void moveToMaxValue() {
52+
while (this.valToNodeMapping.get(this.maxValues.peek()).isEmpty()) {
53+
this.valToNodeMapping.remove(this.maxValues.poll());
3554
}
36-
max.pop();
37-
stack.pop();
38-
while (!temp.isEmpty()) {
39-
push(temp.pop());
55+
}
56+
57+
private void addNodeToStack(Node node) {
58+
Node nextToHead = this.stackHead.next;
59+
nextToHead.prev = node;
60+
node.next = nextToHead;
61+
this.stackHead.next = node;
62+
node.prev = this.stackHead;
63+
}
64+
65+
private void removeNodeFromStack(Node node) {
66+
Node nextNode = node.next;
67+
Node prevNode = node.prev;
68+
prevNode.next = nextNode;
69+
nextNode.prev = prevNode;
70+
}
71+
72+
private static class Node {
73+
int val;
74+
Node next;
75+
Node prev;
76+
77+
public Node(int val) {
78+
this.val = val;
4079
}
41-
return num;
42-
}
80+
}
4381
}
4482

4583
/**

0 commit comments

Comments
 (0)