Skip to content

Commit d6659a9

Browse files
committed
feat: add solutions to lcof problem: No.59.2
1 parent cc8d96a commit d6659a9

File tree

6 files changed

+248
-198
lines changed

6 files changed

+248
-198
lines changed

lcof/面试题59 - II. 队列的最大值/README.md

+141-111
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,17 @@
3737

3838
<!-- 这里可写通用的实现逻辑 -->
3939

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$ 为队列中的元素个数。
4151

4252
<!-- tabs:start -->
4353

@@ -48,25 +58,25 @@
4858
```python
4959
class MaxQueue:
5060
def __init__(self):
51-
self.p = deque()
52-
self.q = deque()
61+
self.q1 = deque()
62+
self.q2 = deque()
5363

5464
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]
5666

5767
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)
6272

6373
def pop_front(self) -> int:
64-
if not self.p:
74+
if not self.q1:
6575
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
7080

7181

7282
# Your MaxQueue object will be instantiated and called as such:
@@ -82,31 +92,34 @@ class MaxQueue:
8292

8393
```java
8494
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<>();
8797

8898
public MaxQueue() {
89-
p = new ArrayDeque<>();
90-
q = new ArrayDeque<>();
99+
91100
}
92101

93102
public int max_value() {
94-
return q.isEmpty() ? -1 : q.peekFirst();
103+
return q2.isEmpty() ? -1 : q2.peek();
95104
}
96105

97106
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();
100109
}
101-
p.offerLast(value);
102-
q.offerLast(value);
110+
q1.offer(value);
111+
q2.offer(value);
103112
}
104113

105114
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;
110123
}
111124
}
112125

@@ -119,131 +132,148 @@ class MaxQueue {
119132
*/
120133
```
121134

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-
174135
### **C++**
175136

176137
```cpp
177138
class MaxQueue {
178-
private:
179-
queue<int> q;
180-
deque<int> d;
181-
182139
public:
183-
MaxQueue() { }
140+
MaxQueue() {
141+
142+
}
184143

185144
int max_value() {
186-
if (d.empty()) return -1;
187-
return d.front();
145+
return q2.empty() ? -1 : q2.front();
188146
}
189147

190148
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);
194154
}
195155

196156
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;
202166
}
167+
168+
private:
169+
queue<int> q1;
170+
deque<int> q2;
203171
};
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+
*/
204180
```
205181

206182
### **Go**
207183

208184
```go
209185
type MaxQueue struct {
210-
queue []int
211-
deque []int
186+
q1, q2 []int
212187
}
213188

214189
func Constructor() MaxQueue {
215-
return MaxQueue{
216-
queue: make([]int, 0),
217-
deque: make([]int, 0),
218-
}
190+
return MaxQueue{[]int{}, []int{}}
219191
}
220192

221193
func (this *MaxQueue) Max_value() int {
222-
if len(this.deque) == 0 {
194+
if len(this.q2) == 0 {
223195
return -1
224196
}
225-
return this.deque[0]
197+
return this.q2[0]
226198
}
227199

228200
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]
231203
}
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)
234206
}
235207

236208
func (this *MaxQueue) Pop_front() int {
237-
if len(this.deque) == 0 {
209+
if len(this.q1) == 0 {
238210
return -1
239211
}
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:]
244216
}
245-
return retVal
217+
return ans
246218
}
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+
*/
247277
```
248278

249279
### **TypeScript**
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,42 @@
11
class MaxQueue {
2-
private:
3-
queue<int> q;
4-
deque<int> d;
5-
62
public:
7-
MaxQueue() {}
3+
MaxQueue() {
84

5+
}
6+
97
int max_value() {
10-
if (d.empty()) return -1;
11-
return d.front();
8+
return q2.empty() ? -1 : q2.front();
129
}
13-
10+
1411
void push_back(int value) {
15-
while (!d.empty() && d.back() < value) d.pop_back();
16-
d.push_back(value);
17-
q.push(value);
12+
while (!q2.empty() && q2.back() < value) {
13+
q2.pop_back();
14+
}
15+
q1.push(value);
16+
q2.push_back(value);
1817
}
19-
18+
2019
int pop_front() {
21-
if (d.empty()) return -1;
22-
int retVal = q.front();
23-
q.pop();
24-
if (d.front() == retVal) d.pop_front();
25-
return retVal;
20+
if (q1.empty()) {
21+
return -1;
22+
}
23+
int ans = q1.front();
24+
q1.pop();
25+
if (q2.front() == ans) {
26+
q2.pop_front();
27+
}
28+
return ans;
2629
}
30+
31+
private:
32+
queue<int> q1;
33+
deque<int> q2;
2734
};
35+
36+
/**
37+
* Your MaxQueue object will be instantiated and called as such:
38+
* MaxQueue* obj = new MaxQueue();
39+
* int param_1 = obj->max_value();
40+
* obj->push_back(value);
41+
* int param_3 = obj->pop_front();
42+
*/

0 commit comments

Comments
 (0)