Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy path716. Max Stack.java
More file actions
Latest commit
executable file
·190 lines (160 loc) · 4.56 KB
/
Copy path716. Max Stack.java
File metadata and controls
executable file
·190 lines (160 loc) · 4.56 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
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, DoublyLinkedList, TreeMap
time: avgO(1), [O(logN) peekMax(), TreeMap]; [O(n) popMax(), TwoStack]
space: O(n)
#### TwoStack
- onetokeepregularelements
- onetorepatthemaxatcurrentstacklevel
- time: O(n) forpopMax() andO(1) fortherestoperations
- space: O(n)
#### TreeMap
- Reference: https://leetcode.com/problems/max-stack/solution/
- UseTreeMaptostore <Int, ListofNodes>, whichgives: **O(logN) insert, deleteandfindMAX**
- Keyreasontouse `DoubleLinkedList` istoperformO(1) removalfor `popMax()`
- Theproblembecomesfindingthetargetvalue & removefromDoubleLinkedList
- time: O(1) forpopMax() andO(logN) fortherest
- 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
*/
classMaxStack {
Stack<Integer> stack;
Stack<Integer> max;
/** initialize your data structure here. */
publicMaxStack() {
stack = newStack<>();
max = newStack<>();
}
publicvoidpush(intx) {
stack.push(x);
intmaxVal = max.isEmpty() ? x : Math.max(max.peek(), x);
max.push(maxVal);
}
publicintpop() {
max.pop();
returnstack.pop();
}
publicinttop() {
returnstack.peek();
}
publicintpeekMax() {
returnmax.peek();
}
publicintpopMax() {
intmaxVal = peekMax();
Stack<Integer> temp = newStack<>();
while(top() != maxVal) temp.push(pop());
pop();
while(!temp.isEmpty()) push(temp.pop());
returnmaxVal;
}
}
/*
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
*/
classMaxStack {
TreeMap<Integer, List<Node>> map;
DoubleLinkedListdll;
publicMaxStack() {
map = newTreeMap();
dll = newDoubleLinkedList();
}
// O(1)
publicvoidpush(intx) {
Nodenode = dll.add(x);
map.putIfAbsent(x, newArrayList<Node>());
map.get(x).add(node);
}
// O(1)
publicintpop() {
intval = dll.pop();
removeFromMap(val);
returnval;
}
// O(1)
publicinttop() {
returndll.peek();
}
// O(logN)
publicintpeekMax() {
returnmap.lastKey();
}
// O(1)
publicintpopMax() {
intmax = peekMax();
Nodenode = removeFromMap(max);
dll.unlink(node);
returnmax;
}
// Find val from map, remove it from list, & remove list if empty
// O(1)
privateNoderemoveFromMap(intval) {
List<Node> list = map.get(val);
Nodenode = list.remove(list.size() - 1);
if (list.isEmpty()) map.remove(val);
returnnode;
}
// Define DoubleLinkedList class
classDoubleLinkedList {
Nodehead, tail;
publicDoubleLinkedList() {
head = newNode(0);
tail = newNode(0);
head.next = tail;
tail.prev = head;
}
publicNodeadd(intval) {
Nodex = newNode(val);
x.next = tail;
x.prev = tail.prev;
tail.prev = tail.prev.next = x; // append to tail
returnx;
}
publicintpop() {
returnunlink(tail.prev).val;
}
publicintpeek() {
returntail.prev.val;
}
publicNodeunlink(Nodenode) {
node.prev.next = node.next;
node.next.prev = node.prev;
returnnode;
}
}
classNode {
intval;
Nodeprev, next;
publicNode(intv) {val = v;}
}
}
```