|
1 | 1 | class MaxStack {
|
2 | 2 |
|
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 | + |
6 | 8 | 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<>(); |
9 | 15 | }
|
10 | 16 |
|
11 | 17 | 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); |
14 | 24 | }
|
15 | 25 |
|
16 | 26 | 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; |
19 | 31 | }
|
20 | 32 |
|
21 | 33 | public int top() {
|
22 |
| - return stack.peek(); |
| 34 | + return this.stackHead.next.val; |
23 | 35 | }
|
24 | 36 |
|
25 | 37 | public int peekMax() {
|
26 |
| - return max.peek(); |
| 38 | + moveToMaxValue(); |
| 39 | + int maxVal = this.maxValues.peek(); |
| 40 | + return this.valToNodeMapping.get(maxVal).peek().val; |
27 | 41 | }
|
28 | 42 |
|
29 | 43 | 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()); |
35 | 54 | }
|
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; |
40 | 79 | }
|
41 |
| - return num; |
42 |
| - } |
| 80 | + } |
43 | 81 | }
|
44 | 82 |
|
45 | 83 | /**
|
|
0 commit comments