37
37
38
38
<!-- 这里可写通用的实现逻辑 -->
39
39
40
- 利用一个辅助队列按单调顺序存储当前队列的最大值。
40
+ ** 方法一:双队列**
41
+
42
+ 我们维护两个队列 $q_1$ 和 $q_2$,其中 $q_1$ 用于存储所有元素,而 $q_2$ 用于存储当前队列中的最大值。
43
+
44
+ 当获取队列中的最大值时,如果队列 $q_2$ 不为空,则队列中的最大值即为 $q_2$ 的队首元素;否则队列为空,返回 $-1$。
45
+
46
+ 当向队列中添加元素时,我们需要将 $q_2$ 弹出所有队尾元素小于当前元素的元素,然后将当前元素添加到 $q_2$ 的队尾,最后将当前元素添加到 $q_1$ 的队尾。
47
+
48
+ 当从队列中弹出元素时,如果 $q_1$ 为空,则返回 $-1$;否则,如果 $q_1$ 的队首元素等于 $q_2$ 的队首元素,则将 $q_2$ 的队首元素弹出,然后将 $q_1$ 的队首元素弹出;否则,只将 $q_1$ 的队首元素弹出。
49
+
50
+ 以上操作的时间复杂度均为 $O(1)$,空间复杂度为 $O(n)$。其中 $n$ 为队列中的元素个数。
41
51
42
52
<!-- tabs:start -->
43
53
48
58
``` python
49
59
class MaxQueue :
50
60
def __init__ (self ):
51
- self .p = deque()
52
- self .q = deque()
61
+ self .q1 = deque()
62
+ self .q2 = deque()
53
63
54
64
def max_value (self ) -> int :
55
- return - 1 if not self .q else self .q [0 ]
65
+ return - 1 if not self .q2 else self .q2 [0 ]
56
66
57
67
def push_back (self , value : int ) -> None :
58
- while self .q and self .q [- 1 ] < value:
59
- self .q .pop()
60
- self .p .append(value)
61
- self .q .append(value)
68
+ while self .q2 and self .q2 [- 1 ] < value:
69
+ self .q2 .pop()
70
+ self .q1 .append(value)
71
+ self .q2 .append(value)
62
72
63
73
def pop_front (self ) -> int :
64
- if not self .p :
74
+ if not self .q1 :
65
75
return - 1
66
- res = self .p .popleft()
67
- if self .q [0 ] == res :
68
- self .q .popleft()
69
- return res
76
+ ans = self .q1 .popleft()
77
+ if self .q2 [0 ] == ans :
78
+ self .q2 .popleft()
79
+ return ans
70
80
71
81
72
82
# Your MaxQueue object will be instantiated and called as such:
@@ -82,31 +92,34 @@ class MaxQueue:
82
92
83
93
``` java
84
94
class MaxQueue {
85
- private Deque<Integer > p ;
86
- private Deque<Integer > q ;
95
+ private Deque<Integer > q1 = new ArrayDeque<> () ;
96
+ private Deque<Integer > q2 = new ArrayDeque<> () ;
87
97
88
98
public MaxQueue () {
89
- p = new ArrayDeque<> ();
90
- q = new ArrayDeque<> ();
99
+
91
100
}
92
101
93
102
public int max_value () {
94
- return q . isEmpty() ? - 1 : q . peekFirst ();
103
+ return q2 . isEmpty() ? - 1 : q2 . peek ();
95
104
}
96
105
97
106
public void push_back (int value ) {
98
- while (! q . isEmpty() && q . peekLast() < value) {
99
- q . pollLast();
107
+ while (! q2 . isEmpty() && q2 . peekLast() < value) {
108
+ q2 . pollLast();
100
109
}
101
- p . offerLast (value);
102
- q . offerLast (value);
110
+ q1 . offer (value);
111
+ q2 . offer (value);
103
112
}
104
113
105
114
public int pop_front () {
106
- if (p. isEmpty()) return - 1 ;
107
- int res = p. pollFirst();
108
- if (q. peek() == res) q. pollFirst();
109
- return res;
115
+ if (q1. isEmpty()) {
116
+ return - 1 ;
117
+ }
118
+ int ans = q1. poll();
119
+ if (q2. peek() == ans) {
120
+ q2. poll();
121
+ }
122
+ return ans;
110
123
}
111
124
}
112
125
@@ -119,131 +132,148 @@ class MaxQueue {
119
132
*/
120
133
```
121
134
122
- ### ** JavaScript**
123
-
124
- ``` js
125
- var MaxQueue = function () {
126
- this .queue = [];
127
- this .maxValue = - Infinity ;
128
- this .maxIdx = - 1 ;
129
- };
130
-
131
- /**
132
- * @return {number}
133
- */
134
- MaxQueue .prototype .max_value = function () {
135
- if (! this .queue .length ) return - 1 ;
136
- return this .maxValue ;
137
- };
138
-
139
- /**
140
- * @param {number} value
141
- * @return {void}
142
- */
143
- MaxQueue .prototype .push_back = function (value ) {
144
- this .queue .push (value);
145
- if (value >= this .maxValue ) {
146
- this .maxIdx = this .queue .length - 1 ;
147
- this .maxValue = value;
148
- }
149
- };
150
-
151
- /**
152
- * @return {number}
153
- */
154
- MaxQueue .prototype .pop_front = function () {
155
- if (! this .queue .length ) return - 1 ;
156
- let a = this .queue .shift ();
157
- this .maxIdx -- ;
158
- if (this .maxIdx < 0 ) {
159
- let tmp = - Infinity ;
160
- let id = - 1 ;
161
- for (let i = 0 ; i < this .queue .length ; i++ ) {
162
- if (this .queue [i] > tmp) {
163
- tmp = this .queue [i];
164
- id = i;
165
- }
166
- }
167
- this .maxIdx = id;
168
- this .maxValue = tmp;
169
- }
170
- return a;
171
- };
172
- ```
173
-
174
135
### ** C++**
175
136
176
137
``` cpp
177
138
class MaxQueue {
178
- private:
179
- queue<int > q;
180
- deque<int > d;
181
-
182
139
public:
183
- MaxQueue() { }
140
+ MaxQueue() {
141
+
142
+ }
184
143
185
144
int max_value () {
186
- if (d.empty()) return -1;
187
- return d.front();
145
+ return q2.empty() ? -1 : q2.front();
188
146
}
189
147
190
148
void push_back(int value) {
191
- while (!d.empty() && d.back() < value) d.pop_back();
192
- d.push_back(value);
193
- q.push(value);
149
+ while (!q2.empty() && q2.back() < value) {
150
+ q2.pop_back();
151
+ }
152
+ q1.push(value);
153
+ q2.push_back(value);
194
154
}
195
155
196
156
int pop_front() {
197
- if (d.empty()) return -1;
198
- int retVal = q.front();
199
- q.pop();
200
- if (d.front() == retVal) d.pop_front();
201
- return retVal;
157
+ if (q1.empty()) {
158
+ return -1;
159
+ }
160
+ int ans = q1.front();
161
+ q1.pop();
162
+ if (q2.front() == ans) {
163
+ q2.pop_front();
164
+ }
165
+ return ans;
202
166
}
167
+
168
+ private:
169
+ queue<int > q1;
170
+ deque<int > q2;
203
171
};
172
+
173
+ /* *
174
+ * Your MaxQueue object will be instantiated and called as such:
175
+ * MaxQueue* obj = new MaxQueue();
176
+ * int param_1 = obj->max_value();
177
+ * obj->push_back(value);
178
+ * int param_3 = obj->pop_front();
179
+ */
204
180
```
205
181
206
182
### ** Go**
207
183
208
184
``` go
209
185
type MaxQueue struct {
210
- queue []int
211
- deque []int
186
+ q1, q2 []int
212
187
}
213
188
214
189
func Constructor () MaxQueue {
215
- return MaxQueue{
216
- queue: make([]int, 0),
217
- deque: make([]int, 0),
218
- }
190
+ return MaxQueue{[]int {}, []int {}}
219
191
}
220
192
221
193
func (this *MaxQueue ) Max_value () int {
222
- if len(this.deque ) == 0 {
194
+ if len (this.q2 ) == 0 {
223
195
return -1
224
196
}
225
- return this.deque [0]
197
+ return this.q2 [0 ]
226
198
}
227
199
228
200
func (this *MaxQueue ) Push_back (value int ) {
229
- for len(this.deque) != 0 && this.deque [len(this.deque )-1] < value {
230
- this.deque = this.deque [:len(this.deque )-1]
201
+ for len (this.q2 ) > 0 && this.q2 [len (this.q2 )-1 ] < value {
202
+ this.q2 = this.q2 [:len (this.q2 )-1 ]
231
203
}
232
- this.deque = append(this.deque , value)
233
- this.queue = append(this.queue , value)
204
+ this.q1 = append (this.q1 , value)
205
+ this.q2 = append (this.q2 , value)
234
206
}
235
207
236
208
func (this *MaxQueue ) Pop_front () int {
237
- if len(this.deque ) == 0 {
209
+ if len (this.q1 ) == 0 {
238
210
return -1
239
211
}
240
- retVal := this.queue [0]
241
- this.queue = this.queue [1:]
242
- if this.deque [0] == retVal {
243
- this.deque = this.deque [1:]
212
+ ans := this.q1 [0 ]
213
+ this.q1 = this.q1 [1 :]
214
+ if this.q2 [0 ] == ans {
215
+ this.q2 = this.q2 [1 :]
244
216
}
245
- return retVal
217
+ return ans
246
218
}
219
+
220
+ /* *
221
+ * Your MaxQueue object will be instantiated and called as such:
222
+ * obj := Constructor();
223
+ * param_1 := obj.Max_value();
224
+ * obj.Push_back(value);
225
+ * param_3 := obj.Pop_front();
226
+ */
227
+ ```
228
+
229
+ ### ** JavaScript**
230
+
231
+ ``` js
232
+ var MaxQueue = function () {
233
+ this .q1 = [];
234
+ this .q2 = [];
235
+ };
236
+
237
+ /**
238
+ * @return {number}
239
+ */
240
+ MaxQueue .prototype .max_value = function () {
241
+ return this .q2 .length ? this .q2 [0 ] : - 1 ;
242
+ };
243
+
244
+ /**
245
+ * @param {number} value
246
+ * @return {void}
247
+ */
248
+ MaxQueue .prototype .push_back = function (value ) {
249
+ while (this .q2 .length && this .q2 [this .q2 .length - 1 ] < value) {
250
+ this .q2 .pop ();
251
+ }
252
+ this .q1 .push (value);
253
+ this .q2 .push (value);
254
+ };
255
+
256
+ /**
257
+ * @return {number}
258
+ */
259
+ MaxQueue .prototype .pop_front = function () {
260
+ if (! this .q1 .length ) {
261
+ return - 1 ;
262
+ }
263
+ const ans = this .q1 .shift ();
264
+ if (this .q2 [0 ] == ans) {
265
+ this .q2 .shift ();
266
+ }
267
+ return ans;
268
+ };
269
+
270
+ /**
271
+ * Your MaxQueue object will be instantiated and called as such:
272
+ * var obj = new MaxQueue()
273
+ * var param_1 = obj.max_value()
274
+ * obj.push_back(value)
275
+ * var param_3 = obj.pop_front()
276
+ */
247
277
```
248
278
249
279
### ** TypeScript**
0 commit comments