1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
| // @Title: 队列的最大值 (队列的最大值 LCOF)
// @Author: 15816537946@163.com
// @Date: 2022-03-02 22:55:12
// @Runtime: 72 ms
// @Memory: 7.9 MB
type MaxQueue struct {
q []int
p []int
}
func Constructor() MaxQueue {
return MaxQueue{}
}
func (this *MaxQueue) Max_value() int {
if len(this.q) == 0 {
return -1
}
return this.p[0]
}
func (this *MaxQueue) Push_back(value int) {
this.q = append(this.q,value)
for len(this.p) > 0 && value > this.p[len(this.p)-1] {
this.p = this.p[:len(this.p)-1]
}
this.p = append(this.p, value)
}
func (this *MaxQueue) Pop_front() int {
if len(this.q) == 0 {
return -1
}
if this.p[0] == this.q[0] {
this.p = this.p[1:]
}
value := this.q[0]
this.q = this.q[1:]
return value
}
/**
* Your MaxQueue object will be instantiated and called as such:
* obj := Constructor();
* param_1 := obj.Max_value();
* obj.Push_back(value);
* param_3 := obj.Pop_front();
*/
|