原题链接:https://leetcode-cn.com/problems/dui-lie-de-zui-da-zhi-lcof/
deque<int> Q1;
deque<int> Q2;
MaxQueue() {
}
int max_value() {
if (Q1.empty()) return -1;
return Q2.front();
}
void push_back(int value) {
Q1.push_back(value);
while (!Q2.empty() && Q2.back() < value) {
Q2.pop_back();
}
Q2.push_back(value);
}
int pop_front() {
if (Q1.empty()) return -1;
if (!Q2.empty() && Q2.front() == Q1.front()) Q2.pop_front();
int t = Q1.front();
Q1.pop_front();
return t;
}
type MaxQueue struct {
data []int
ma []int
}
func Constructor() MaxQueue {
return MaxQueue{
data: make([]int, 0),
ma: make([]int, 0),
}
}
func (this *MaxQueue) Max_value() int {
if len(this.ma) == 0 {
return -1
}
return this.ma[0]
}
func (this *MaxQueue) Push_back(value int) {
for len(this.ma) != 0 && this.ma[len(this.ma)-1] < value {
this.ma = this.ma[:len(this.ma)-1]
}
this.data = append(this.data, value)
this.ma = append(this.ma, value)
}
func (this *MaxQueue) Pop_front() int { if len(this.data) <= 0 { return -1 }
d := this.data[0]
this.data = this.data[1:]
if d == this.ma[0] {
this.ma = this.ma[1:]
}
return d
}