@@ -65,22 +65,256 @@ stk.top(); // 返回 5,[<strong>5</strong>] - 栈没有改变
65
65
66
66
<!-- 这里可写通用的实现逻辑 -->
67
67
68
+ ** 方法一:双向链表 + 有序集合**
69
+
70
+ 使用双向链表存储栈中的元素,使用有序集合存储栈中的元素,有序集合中的元素按照从小到大的顺序存储,每个元素都对应着双向链表中的一个节点。
71
+
72
+ - 调用 ` push(x) ` 方法时,将元素 ` x ` 插入到双向链表的末尾,同时将元素 ` x ` 对应的节点插入到有序集合中。时间复杂度 $O(\log n)$。
73
+ - 调用 ` pop() ` 方法时,将双向链表的末尾节点删除,同时将有序集合中的对应节点删除。时间复杂度 $O(\log n)$。
74
+ - 调用 ` top() ` 方法时,返回双向链表的末尾节点的值。时间复杂度 $O(1)$。
75
+ - 调用 ` peekMax() ` 方法时,返回有序集合中的最后一个元素对应的节点的值。时间复杂度 $O(\log n)$。
76
+ - 调用 ` popMax() ` 方法时,将有序集合中的最后一个元素删除,同时将对应的节点从双向链表中删除。时间复杂度 $O(\log n)$。
77
+
78
+ 空间复杂度 $O(n)$。其中 $n$ 为栈中的元素个数。
79
+
68
80
<!-- tabs:start -->
69
81
70
82
### ** Python3**
71
83
72
84
<!-- 这里可写当前语言的特殊实现逻辑 -->
73
85
74
86
``` python
87
+ from sortedcontainers import SortedList
88
+
89
+
90
+ class Node :
91
+ def __init__ (self , val = 0 ):
92
+ self .val = val
93
+ self .prev: Union[Node, None ] = None
94
+ self .next: Union[Node, None ] = None
95
+
96
+
97
+ class DoubleLinkedList :
98
+ def __init__ (self ):
99
+ self .head = Node()
100
+ self .tail = Node()
101
+ self .head.next = self .tail
102
+ self .tail.prev = self .head
103
+
104
+ def append (self , val ) -> Node:
105
+ node = Node(val)
106
+ node.next = self .tail
107
+ node.prev = self .tail.prev
108
+ self .tail.prev = node
109
+ node.prev.next = node
110
+ return node
111
+
112
+ @ staticmethod
113
+ def remove (node ) -> Node:
114
+ node.prev.next = node.next
115
+ node.next.prev = node.prev
116
+ return node
117
+
118
+ def pop (self ) -> Node:
119
+ return self .remove(self .tail.prev)
120
+
121
+ def peek (self ):
122
+ return self .tail.prev.val
75
123
124
+
125
+ class MaxStack :
126
+
127
+ def __init__ (self ):
128
+ self .stk = DoubleLinkedList()
129
+ self .sl = SortedList(key = lambda x : x.val)
130
+
131
+ def push (self , x : int ) -> None :
132
+ node = self .stk.append(x)
133
+ self .sl.add(node)
134
+
135
+ def pop (self ) -> int :
136
+ node = self .stk.pop()
137
+ self .sl.remove(node)
138
+ return node.val
139
+
140
+ def top (self ) -> int :
141
+ return self .stk.peek()
142
+
143
+ def peekMax (self ) -> int :
144
+ return self .sl[- 1 ].val
145
+
146
+ def popMax (self ) -> int :
147
+ node = self .sl.pop()
148
+ DoubleLinkedList.remove(node)
149
+ return node.val
150
+
151
+ # Your MaxStack object will be instantiated and called as such:
152
+ # obj = MaxStack()
153
+ # obj.push(x)
154
+ # param_2 = obj.pop()
155
+ # param_3 = obj.top()
156
+ # param_4 = obj.peekMax()
157
+ # param_5 = obj.popMax()
76
158
```
77
159
78
160
### ** Java**
79
161
80
162
<!-- 这里可写当前语言的特殊实现逻辑 -->
81
163
82
164
``` java
165
+ class Node {
166
+ public int val;
167
+ public Node prev, next;
168
+
169
+ public Node () {
170
+
171
+ }
172
+
173
+ public Node (int val ) {
174
+ this . val = val;
175
+ }
176
+ }
177
+
178
+ class DoubleLinkedList {
179
+ private final Node head = new Node ();
180
+ private final Node tail = new Node ();
181
+
182
+ public DoubleLinkedList () {
183
+ head. next = tail;
184
+ tail. prev = head;
185
+ }
186
+
187
+ public Node append (int val ) {
188
+ Node node = new Node (val);
189
+ node. next = tail;
190
+ node. prev = tail. prev;
191
+ tail. prev = node;
192
+ node. prev. next = node;
193
+ return node;
194
+ }
195
+
196
+ public static Node remove (Node node ) {
197
+ node. prev. next = node. next;
198
+ node. next. prev = node. prev;
199
+ return node;
200
+ }
201
+
202
+ public Node pop () {
203
+ return remove(tail. prev);
204
+ }
205
+
206
+ public int peek () {
207
+ return tail. prev. val;
208
+ }
209
+ }
210
+
211
+ class MaxStack {
212
+ private DoubleLinkedList stk = new DoubleLinkedList ();
213
+ private TreeMap<Integer , List<Node > > tm = new TreeMap<> ();
214
+
215
+ public MaxStack () {
216
+
217
+ }
218
+
219
+ public void push (int x ) {
220
+ Node node = stk. append(x);
221
+ tm. computeIfAbsent(x, k - > new ArrayList<> ()). add(node);
222
+ }
223
+
224
+ public int pop () {
225
+ Node node = stk. pop();
226
+ List<Node > nodes = tm. get(node. val);
227
+ int x = nodes. remove(nodes. size() - 1 ). val;
228
+ if (nodes. isEmpty()) {
229
+ tm. remove(node. val);
230
+ }
231
+ return x;
232
+ }
233
+
234
+ public int top () {
235
+ return stk. peek();
236
+ }
237
+
238
+ public int peekMax () {
239
+ return tm. lastKey();
240
+ }
241
+
242
+ public int popMax () {
243
+ int x = peekMax();
244
+ List<Node > nodes = tm. get(x);
245
+ Node node = nodes. remove(nodes. size() - 1 );
246
+ if (nodes. isEmpty()) {
247
+ tm. remove(x);
248
+ }
249
+ DoubleLinkedList . remove(node);
250
+ return x;
251
+ }
252
+ }
253
+
254
+ /**
255
+ * Your MaxStack object will be instantiated and called as such:
256
+ * MaxStack obj = new MaxStack();
257
+ * obj.push(x);
258
+ * int param_2 = obj.pop();
259
+ * int param_3 = obj.top();
260
+ * int param_4 = obj.peekMax();
261
+ * int param_5 = obj.popMax();
262
+ */
263
+ ```
264
+
265
+ ### ** C++**
266
+
267
+ ``` cpp
268
+ class MaxStack {
269
+ public:
270
+ MaxStack() {
271
+ }
272
+
273
+ void push(int x) {
274
+ stk.push_back(x);
275
+ tm.insert({x, --stk.end()});
276
+ }
277
+
278
+ int pop () {
279
+ auto it = --stk.end();
280
+ int ans = *it;
281
+ auto mit = --tm.upper_bound(ans);
282
+ tm.erase(mit);
283
+ stk.erase(it);
284
+ return ans;
285
+ }
286
+
287
+ int top() {
288
+ return stk.back();
289
+ }
290
+
291
+ int peekMax() {
292
+ return tm.rbegin()->first;
293
+ }
294
+
295
+ int popMax() {
296
+ auto mit = --tm.end();
297
+ auto it = mit->second;
298
+ int ans = *it;
299
+ tm.erase(mit);
300
+ stk.erase(it);
301
+ return ans;
302
+ }
303
+
304
+ private:
305
+ multimap<int, list<int >::iterator> tm;
306
+ list<int > stk;
307
+ };
83
308
309
+ /* *
310
+ * Your MaxStack object will be instantiated and called as such:
311
+ * MaxStack* obj = new MaxStack();
312
+ * obj->push(x);
313
+ * int param_2 = obj->pop();
314
+ * int param_3 = obj->top();
315
+ * int param_4 = obj->peekMax();
316
+ * int param_5 = obj->popMax();
317
+ */
84
318
```
85
319
86
320
### ** ...**
0 commit comments