algorithm-base/animation-simulation/单调队列单调栈/剑指offer59队列的最大值.md

145 lines
5.2 KiB
Java
Raw Normal View History

2021-07-23 15:44:19 +00:00
> **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
2021-03-20 11:38:55 +00:00
>
>
>
2021-07-23 15:44:19 +00:00
> <u>[****](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
2021-03-20 11:38:55 +00:00
2021-03-19 08:36:59 +00:00
##
2021-03-20 11:38:55 +00:00
#### [ Offer 59 - II. ](https://leetcode-cn.com/problems/dui-lie-de-zui-da-zhi-lcof/)
2021-03-19 08:36:59 +00:00
####
max_value
pop_front max_value -1
** 1**
2021-07-23 15:44:19 +00:00
> : ["MaxQueue","push_back","push_back","max_value","pop_front","max_value"] > [[],[1],[2],[],[],[]]
2021-03-19 08:36:59 +00:00
> : [null,null,null,2,1,2]
** 2**
2021-07-23 15:44:19 +00:00
> :
> ["MaxQueue","pop_front","max_value"] > [[],[],[]]
2021-03-19 08:36:59 +00:00
> : [null,-1,-1]
####
1
![](https://cdn.jsdelivr.net/gh/tan45du/github.io.phonto2@master/myphoto/队列的最大值.6bfapy4zf1g0.png)
![](https://img-blog.csdnimg.cn/20210319154950406.gif)
max_value
![](https://img-blog.csdnimg.cn/20210319154716931.gif)
1.
2.
3.
```java
class MaxQueue {
//普通队列
Queue<Integer> que;
//双端队列
Deque<Integer> deq;
public MaxQueue() {
que = new LinkedList<>();
deq = new LinkedList<>();
2021-07-23 15:44:19 +00:00
}
//获取最大值值,返回我们双端队列的对头即可,因为我们双端队列是单调递减的嘛
2021-03-19 08:36:59 +00:00
public int max_value() {
2021-07-23 15:44:19 +00:00
return deq.isEmpty() ? -1 : deq.peekFirst();
2021-03-19 08:36:59 +00:00
}
//入队操作
public void push_back(int value) {
que.offer(value);
//维护单调递减
while (!deq.isEmpty() && value > deq.peekLast()){
deq. pollLast();
}
deq.offerLast(value);
}
//返回队头元素此时有个细节我们需要用equals
//这里需要使用 equals() 代替 == 因为队列中存储的是 int 的包装类 Integer
public int pop_front() {
if(que.isEmpty()) return -1;
if (que.peek().equals(deq.peekFirst())) {
2021-07-23 15:44:19 +00:00
deq.pollFirst();
2021-03-19 08:36:59 +00:00
}
return que.poll();
}
}
```
2021-07-27 18:26:32 +00:00
GO Code:
```go
type MaxQueue struct {
que []int // 普通队列
deq []int // 双端队列
size int // que的队列长度
}
func Constructor() MaxQueue {
return MaxQueue{
que: []int{},
deq: []int{},
}
}
// Is_empty 表示队列是否为空
func (mq *MaxQueue) Is_empty() bool {
return mq.size == 0
}
2021-07-29 02:33:38 +00:00
// Max_value 取最大值值,返回我们双端队列的对头即可,因为我们双端队列是单调递减的嘛
2021-07-27 18:26:32 +00:00
func (mq *MaxQueue) Max_value() int {
if mq.Is_empty() { return -1 }
return mq.deq[0]
}
// Push_back 入队
func (mq *MaxQueue) Push_back(value int) {
mq.que = append(mq.que, value)
// 维护单调递减队列
for len(mq.deq) != 0 && mq.deq[len(mq.deq) - 1] < value {
mq.deq = mq.deq[:len(mq.deq) - 1]
}
mq.deq = append(mq.deq, value)
mq.size++
}
// Pop_front 弹出队列头元素,并且返回其值。
func (mq *MaxQueue) Pop_front() int {
if mq.Is_empty() { return -1 }
ans := mq.que[0]
mq.que = mq.que[1:]
if mq.deq[0] == ans {
mq.deq = mq.deq[1:]
}
mq.size--
return ans
}
```
2021-07-23 15:44:19 +00:00
###