-
Notifications
You must be signed in to change notification settings - Fork 22
/
716. Max Stack.java
executable file
·190 lines (160 loc) · 4.56 KB
/
716. Max Stack.java
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
M
tags: Design, Stack, Doubly Linked List, TreeMap
time: avg O(1), [O(logN) peekMax(), TreeMap]; [O(n) popMax(), TwoStack]
space: O(n)
#### Two Stack
- one to keep regular elements
- one to repat the max at current stack level
- time: O(n) for popMax() and O(1) for the rest operations
- space: O(n)
#### TreeMap
- Reference: https://leetcode.com/problems/max-stack/solution/
- Use TreeMap to store <Int, List of Nodes>, which gives: **O(logN) insert, delete and find MAX**
- Key reason to use `DoubleLinkedList` is to perform O(1) removal for `popMax()`
- The problem becomes finding the target value & remove from DoubleLinkedList
- time: O(1) for popMax() and O(logN) for the rest
- space: O(n)
```
/*
Design a max stack that supports push, pop, top, peekMax and popMax.
push(x) -- Push element x onto stack.
pop() -- Remove the element on top of the stack and return it.
top() -- Get the element on the top.
peekMax() -- Retrieve the maximum element in the stack.
popMax() -- Retrieve the maximum element in the stack, and remove it. If you find more than one maximum elements, only remove the top-most one.
Example 1:
MaxStack stack = new MaxStack();
stack.push(5);
stack.push(1);
stack.push(5);
stack.top(); -> 5
stack.popMax(); -> 5
stack.top(); -> 1
stack.peekMax(); -> 5
stack.pop(); -> 1
stack.top(); -> 5
Note:
-1e7 <= x <= 1e7
Number of operations won't exceed 10000.
The last four operations won't be called when stack is empty.
*/
/*
Use two stacks:
- one to keep regular elements
- one to repat the max at current stack level
*/
class MaxStack {
Stack<Integer> stack;
Stack<Integer> max;
/** initialize your data structure here. */
public MaxStack() {
stack = new Stack<>();
max = new Stack<>();
}
public void push(int x) {
stack.push(x);
int maxVal = max.isEmpty() ? x : Math.max(max.peek(), x);
max.push(maxVal);
}
public int pop() {
max.pop();
return stack.pop();
}
public int top() {
return stack.peek();
}
public int peekMax() {
return max.peek();
}
public int popMax() {
int maxVal = peekMax();
Stack<Integer> temp = new Stack<>();
while(top() != maxVal) temp.push(pop());
pop();
while(!temp.isEmpty()) push(temp.pop());
return maxVal;
}
}
/*
Reference: https://leetcode.com/problems/max-stack/solution/
- Use TreeMap to store <Int, List of Nodes>, which gives O(logN) insert, delete and find MAX
- Build DoubleLinkedList class to perform O(1) removal
- The problem becomes finding the target value & remove from DoubleLinkedList
*/
class MaxStack {
TreeMap<Integer, List<Node>> map;
DoubleLinkedList dll;
public MaxStack() {
map = new TreeMap();
dll = new DoubleLinkedList();
}
// O(1)
public void push(int x) {
Node node = dll.add(x);
map.putIfAbsent(x, new ArrayList<Node>());
map.get(x).add(node);
}
// O(1)
public int pop() {
int val = dll.pop();
removeFromMap(val);
return val;
}
// O(1)
public int top() {
return dll.peek();
}
// O(logN)
public int peekMax() {
return map.lastKey();
}
// O(1)
public int popMax() {
int max = peekMax();
Node node = removeFromMap(max);
dll.unlink(node);
return max;
}
// Find val from map, remove it from list, & remove list if empty
// O(1)
private Node removeFromMap(int val) {
List<Node> list = map.get(val);
Node node = list.remove(list.size() - 1);
if (list.isEmpty()) map.remove(val);
return node;
}
// Define DoubleLinkedList class
class DoubleLinkedList {
Node head, tail;
public DoubleLinkedList() {
head = new Node(0);
tail = new Node(0);
head.next = tail;
tail.prev = head;
}
public Node add(int val) {
Node x = new Node(val);
x.next = tail;
x.prev = tail.prev;
tail.prev = tail.prev.next = x; // append to tail
return x;
}
public int pop() {
return unlink(tail.prev).val;
}
public int peek() {
return tail.prev.val;
}
public Node unlink(Node node) {
node.prev.next = node.next;
node.next.prev = node.prev;
return node;
}
}
class Node {
int val;
Node prev, next;
public Node(int v) {val = v;}
}
}
```