剑指offer59-Ⅱ 队列的最大值【设计、队列】

题目描述

请定义一个队列并实现函数 max_value 得到队列里的最大值,要求函数max_valuepush_backpop_front均摊时间复杂度都是O(1)。

若队列为空,pop_frontmax_value 需要返回 -1

示例 1:

1
2
3
4
输入: 
["MaxQueue","push_back","push_back","max_value","pop_front","max_value"]
[[],[1],[2],[],[],[]]
输出: [null,null,null,2,1,2]

示例 2:

1
2
3
4
输入: 
["MaxQueue","pop_front","max_value"]
[[],[],[]]
输出: [null,-1,-1]

限制:

  • 1 <= push_back,pop_front,max_value的总操作数 <= 10000
  • 1 <= value <= 10^5

解题思路

方法一:队列

使用双端队列来保存最大值,如果加入的值比双端队列的尾部大,则将尾部元素退出,尾部元素大于等于即将加入的值或者双端队列为

代码实现

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
class MaxQueue {
Queue<Integer> q;
Deque<Integer> d;

public MaxQueue() {
q = new LinkedList<Integer>();
d = new LinkedList<Integer>();
}

public int max_value() {
if (d.isEmpty()) {
return -1;
}
return d.peekFirst();
}

public void push_back(int value) {
// 将加入的值加入到双端队列中
while (!d.isEmpty() && d.peekLast() < value) {
d.pollLast();
}

d.offerLast(value);
q.offer(value);
}

public int pop_front() {
if (q.isEmpty()) {
return -1;
}
int ans = q.poll();
if (ans == d.peekFirst()) {
d.pollFirst();
}
return ans;
}
}

复杂度分析

max_valuepush_backpop_front均摊时间复杂度都是O(1)。

空间复杂度:O(n)

资料

剑指offer59-Ⅱ 队列的最大值【设计、队列】

http://example.com/2021/06/05/剑指offer59-Ⅱ-队列的最大值/

Author

John Doe

Posted on

2021-06-05

Updated on

2021-06-08

Licensed under

You need to set install_url to use ShareThis. Please set it in _config.yml.
You forgot to set the business or currency_code for Paypal. Please set it in _config.yml.

Comments

You forgot to set the shortname for Disqus. Please set it in _config.yml.