File tree Expand file tree Collapse file tree 5 files changed +140
-7
lines changed Expand file tree Collapse file tree 5 files changed +140
-7
lines changed Original file line number Diff line number Diff line change 5858
5959非常感谢以下所有朋友对本项目的贡献,你们是最可爱的人!
6060
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 >
6262
6363## 赞助人
6464
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 >
6666
6767> You help the developer community practice for interviews, and there is nothing better we could ask for. -- [ Alan Yessenbayev] ( https://opencollective.com/alan-yessenbayev )
6868
6969## 赞助商
7070
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 >
7272
7373## 许可证
7474
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.
6060
6161## Backers
6262
63+ Thank you to all our backers!
64+
6365<a href =" https://opencollective.com/doocs-leetcode#backers " target =" _blank " ><img src =" https://opencollective.com/doocs-leetcode/backers.svg?width=890 " ></a >
6466
6567> You help the developer community practice for interviews, and there is nothing better we could ask for. -- [ Alan Yessenbayev] ( https://opencollective.com/alan-yessenbayev )
6668
6769## Sponsors
6870
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 >
7074
7175## License
7276
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 2525```
2626
2727** 限制:**
28+
2829- ` 1 <= push_back,pop_front,max_value的总操作数 <= 10000 `
2930- ` 1 <= value <= 10^5 `
3031
3132## 解法
3233<!-- 这里可写通用的实现逻辑 -->
3334
35+ 利用一个辅助队列按单调顺序存储当前队列的最大值。
36+
3437
3538### Python3
3639<!-- 这里可写当前语言的特殊实现逻辑 -->
3740``` 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
3963```
4064
4165### Java
4266<!-- 这里可写当前语言的特殊实现逻辑 -->
4367``` java
68+ class MaxQueue {
69+
70+ private Queue<Integer > p = new ArrayDeque<> ();
71+ private Deque<Integer > q = new ArrayDeque<> ();
4472
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+ */
45108```
46109
47110### 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