-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathoffer592.java
More file actions
57 lines (51 loc) · 1.62 KB
/
offer592.java
File metadata and controls
57 lines (51 loc) · 1.62 KB
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
54
55
56
57
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Queue;
/**
* [59-II] 队列的最大值
*
* 题目: 请定义一个队列并实现函数 max_value 得到队列里的最大值, 要求函数 max_value, push_back 和 pop_front 的时间复杂度都是O(1).
* 若队列为空, pop_front 和 max_value 需要返回 -1.
*
* 思路: 本质上是一个求滑动窗口最大值的问题. 将这个队列可以看成是一个滑动窗口, 入队就是将窗口的右边界右移, 出队就是将窗口的左边界右移.
*/
class MaxQueue {
private Queue<Integer> data;
private Deque<Integer> max;
public MaxQueue() {
data = new ArrayDeque<>();
max = new ArrayDeque<>();
}
public int max_value() {
if (max.isEmpty()) {
return -1;
}
return max.peekFirst();
}
public void push_back(int value) {
data.offer(value);
// maintain max queue's head element is current data queue's maximum value.
while (!max.isEmpty() && value > max.peekLast()) {
max.pollLast();
}
max.addLast(value);
}
public int pop_front() {
if (data.isEmpty()) {
return -1;
}
int ret = data.poll();
// maintain max queue's head element is current data queue's maximum value.
if (ret == max.peekFirst()) {
max.pollFirst();
}
return ret;
}
}
/**
* Your MaxQueue object will be instantiated and called as such:
* MaxQueue obj = new MaxQueue();
* int param_1 = obj.max_value();
* obj.push_back(value);
* int param_3 = obj.pop_front();
*/