File tree 5 files changed +140
-7
lines changed
5 files changed +140
-7
lines changed Original file line number Diff line number Diff line change 58
58
59
59
非常感谢以下所有朋友对本项目的贡献,你们是最可爱的人!
60
60
61
- <a href =" https://opencollective.com/doocs-leetcode/contributors.svg?width=890&button=true " ><img src =" https://opencollective.com/doocs-leetcode/contributors.svg?width=1200&button=false " /></a >
61
+ <a href =" https://opencollective.com/doocs-leetcode/contributors.svg?width=890&button=true " target = " _blank " ><img src =" https://opencollective.com/doocs-leetcode/contributors.svg?width=1200&button=false " /></a >
62
62
63
63
## 赞助人
64
64
65
- <a href =" https://opencollective.com/doocs-leetcode# backers " target =" _blank " ><img src =" https://opencollective.com/doocs-leetcode/backers.svg?width=890 " ></a >
65
+ <a href =" https://opencollective.com/doocs-leetcode/ backers.svg?width=890 " target =" _blank " ><img src =" https://opencollective.com/doocs-leetcode/backers.svg?width=890 " ></a >
66
66
67
67
> You help the developer community practice for interviews, and there is nothing better we could ask for. -- [ Alan Yessenbayev] ( https://opencollective.com/alan-yessenbayev )
68
68
69
69
## 赞助商
70
70
71
- <a href =" https://opencollective.com/doocs-leetcode/sponsor/0/website " target =" _blank " ><img src =" https://opencollective.com/doocs-leetcode/sponsor/0/avatar .svg " ></a >
71
+ <a href =" https://opencollective.com/doocs-leetcode/sponsors.svg?width=890 " target =" _blank " ><img src =" https://opencollective.com/doocs-leetcode/sponsors .svg?width=890 " ></a >
72
72
73
73
## 许可证
74
74
75
- <a rel =" license " href =" https://github.com/doocs/leetcode/blob/master/LICENSE " ><img alt =" Creative Commons License " style =" border-width :0 " src =" ./img/cc-by-sa-88x31.png " /></a ><br /><a rel =" license " href =" http://creativecommons.org/licenses/by-sa/4.0/ " >知识共享 版权归属-相同方式共享 4.0 国际 公共许可证</a >
75
+ <a rel =" license " href =" http://creativecommons.org/licenses/by-sa/4.0/ " >知识共享 版权归属-相同方式共享 4.0 国际 公共许可证</a >
76
+
77
+ <a rel =" license " href =" https://github.com/doocs/leetcode/blob/master/LICENSE " ><img alt =" Creative Commons License " style =" border-width :0 " src =" ./img/cc-by-sa-88x31.png " /></a >
Original file line number Diff line number Diff line change @@ -60,14 +60,20 @@ This project exists thanks to all the people who contribute.
60
60
61
61
## Backers
62
62
63
+ Thank you to all our backers!
64
+
63
65
<a href =" https://opencollective.com/doocs-leetcode#backers " target =" _blank " ><img src =" https://opencollective.com/doocs-leetcode/backers.svg?width=890 " ></a >
64
66
65
67
> You help the developer community practice for interviews, and there is nothing better we could ask for. -- [ Alan Yessenbayev] ( https://opencollective.com/alan-yessenbayev )
66
68
67
69
## Sponsors
68
70
69
- <a href =" https://opencollective.com/doocs-leetcode/sponsor/0/website " target =" _blank " ><img src =" https://opencollective.com/doocs-leetcode/sponsor/0/avatar.svg " ></a >
71
+ Support this project by becoming a sponsor. Your logo will show up here with a link to your website.
72
+
73
+ <a href =" https://opencollective.com/doocs-leetcode#sponsors " target =" _blank " ><img src =" https://opencollective.com/doocs-leetcode/sponsors.svg?width=890 " ></a >
70
74
71
75
## License
72
76
73
- <a rel =" license " href =" https://github.com/doocs/leetcode/blob/master/LICENSE " ><img alt =" Creative Commons License " style =" border-width :0 " src =" ./img/cc-by-sa-88x31.png " /></a ><br />This work is licensed under a <a rel =" license " href =" http://creativecommons.org/licenses/by-sa/4.0/ " >Creative Commons Attribution-ShareAlike 4.0 International License</a >.
77
+ This work is licensed under a <a rel =" license " href =" http://creativecommons.org/licenses/by-sa/4.0/ " >Creative Commons Attribution-ShareAlike 4.0 International License</a >.
78
+
79
+ <a rel =" license " href =" https://github.com/doocs/leetcode/blob/master/LICENSE " ><img alt =" Creative Commons License " style =" border-width :0 " src =" ./img/cc-by-sa-88x31.png " /></a >
Original file line number Diff line number Diff line change 25
25
```
26
26
27
27
** 限制:**
28
+
28
29
- ` 1 <= push_back,pop_front,max_value的总操作数 <= 10000 `
29
30
- ` 1 <= value <= 10^5 `
30
31
31
32
## 解法
32
33
<!-- 这里可写通用的实现逻辑 -->
33
34
35
+ 利用一个辅助队列按单调顺序存储当前队列的最大值。
36
+
34
37
35
38
### Python3
36
39
<!-- 这里可写当前语言的特殊实现逻辑 -->
37
40
``` python
38
-
41
+ class MaxQueue :
42
+
43
+ def __init__ (self ):
44
+ self .p = []
45
+ self .q = []
46
+
47
+ def max_value (self ) -> int :
48
+ return - 1 if not self .q else self .q[0 ]
49
+
50
+ def push_back (self , value : int ) -> None :
51
+ while self .q and self .q[- 1 ] < value:
52
+ self .q.pop(- 1 )
53
+ self .q.append(value)
54
+ self .p.append(value)
55
+
56
+
57
+ def pop_front (self ) -> int :
58
+ if not self .p: return - 1
59
+ res = self .p.pop(0 )
60
+ if res == self .q[0 ]:
61
+ self .q.pop(0 )
62
+ return res
39
63
```
40
64
41
65
### Java
42
66
<!-- 这里可写当前语言的特殊实现逻辑 -->
43
67
``` java
68
+ class MaxQueue {
69
+
70
+ private Queue<Integer > p = new ArrayDeque<> ();
71
+ private Deque<Integer > q = new ArrayDeque<> ();
44
72
73
+ public MaxQueue () {
74
+
75
+ }
76
+
77
+ public int max_value () {
78
+ return q. isEmpty() ? - 1 : q. peekFirst();
79
+ }
80
+
81
+ public void push_back (int value ) {
82
+ while (! q. isEmpty() && q. peekLast() < value) {
83
+ q. pollLast();
84
+ }
85
+ q. addLast(value);
86
+ p. add(value);
87
+ }
88
+
89
+ public int pop_front () {
90
+ if (p. isEmpty()) {
91
+ return - 1 ;
92
+ }
93
+ int res = p. poll();
94
+ if (res == q. peekFirst()) {
95
+ q. pollFirst();
96
+ }
97
+ return res;
98
+ }
99
+ }
100
+
101
+ /**
102
+ * Your MaxQueue object will be instantiated and called as such:
103
+ * MaxQueue obj = new MaxQueue();
104
+ * int param_1 = obj.max_value();
105
+ * obj.push_back(value);
106
+ * int param_3 = obj.pop_front();
107
+ */
45
108
```
46
109
47
110
### JavaScript
Original file line number Diff line number Diff line change
1
+ class MaxQueue {
2
+
3
+ private Queue <Integer > p = new ArrayDeque <>();
4
+ private Deque <Integer > q = new ArrayDeque <>();
5
+
6
+ public MaxQueue () {
7
+
8
+ }
9
+
10
+ public int max_value () {
11
+ return q .isEmpty () ? -1 : q .peekFirst ();
12
+ }
13
+
14
+ public void push_back (int value ) {
15
+ while (!q .isEmpty () && q .peekLast () < value ) {
16
+ q .pollLast ();
17
+ }
18
+ q .addLast (value );
19
+ p .add (value );
20
+ }
21
+
22
+ public int pop_front () {
23
+ if (p .isEmpty ()) {
24
+ return -1 ;
25
+ }
26
+ int res = p .poll ();
27
+ if (res == q .peekFirst ()) {
28
+ q .pollFirst ();
29
+ }
30
+ return res ;
31
+ }
32
+ }
33
+
34
+ /**
35
+ * Your MaxQueue object will be instantiated and called as such:
36
+ * MaxQueue obj = new MaxQueue();
37
+ * int param_1 = obj.max_value();
38
+ * obj.push_back(value);
39
+ * int param_3 = obj.pop_front();
40
+ */
Original file line number Diff line number Diff line change
1
+ class MaxQueue :
2
+
3
+ def __init__ (self ):
4
+ self .p = []
5
+ self .q = []
6
+
7
+ def max_value (self ) -> int :
8
+ return - 1 if not self .q else self .q [0 ]
9
+
10
+ def push_back (self , value : int ) -> None :
11
+ while self .q and self .q [- 1 ] < value :
12
+ self .q .pop (- 1 )
13
+ self .q .append (value )
14
+ self .p .append (value )
15
+
16
+
17
+ def pop_front (self ) -> int :
18
+ if not self .p : return - 1
19
+ res = self .p .pop (0 )
20
+ if res == self .q [0 ]:
21
+ self .q .pop (0 )
22
+ return res
You can’t perform that action at this time.
0 commit comments