-
Notifications
You must be signed in to change notification settings - Fork 1
/
Q59_maxSlidingWindow.java
127 lines (103 loc) · 3.27 KB
/
Q59_maxSlidingWindow.java
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package jianzhi_offer;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
import java.util.PriorityQueue;
/**
* @author weiyuwang
* @since 2019/2/3 21:21
*/
public class Q59_maxSlidingWindow {
public static void main(String[] args) {
System.out.println(Arrays.toString(maxSlidingWindow2(new int[]{1, 3, -1, -3, 5, 3, 6, 7}, 3)));
}
/**
* LeetCode 239
* <p>
* 给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口 k 内的数字。滑动窗口每次只向右移动一位。
* <p>
* 返回滑动窗口最大值
*/
public int[] maxSlidingWindow(int[] nums, int k) {
if (nums == null || nums.length == 0) return new int[]{};
PriorityQueue<Integer> queue = new PriorityQueue<>((o1, o2) -> {
return o2 - o1;
});
int[] res = new int[nums.length - k + 1];
int index = 0, start = 0, end = k - 1;
for (int i = 0; i < k; i++) {
queue.add(nums[i]);
} // 初始化优先队列
while (end < nums.length) {
res[index++] = queue.peek();
if (end == nums.length - 1) break;
//移动
queue.remove(nums[start++]);
queue.add(nums[++end]);
}
return res;
}
public static int[] maxSlidingWindow2(int[] nums, int k) {
if (k == 1 || nums == null || nums.length == 0) {
return nums;
}
int[] result = new int[nums.length - k + 1];
int resultIndex = 0;
int max = Integer.MIN_VALUE;
int start = 0;
int end = k - 1;
for (int i = start; i <= end; i++) {
if (nums[i] > max) {
max = nums[i];
}
}
result[resultIndex++] = max;
while (++end < nums.length) {
int oldMax = max;
if (nums[end] > max) {
max = nums[end];
}
int oldStart = start;
start += 1;
if (oldMax == max && nums[oldStart] == max) {
max = nums[start];
for (int i = start; i <= end; i++) {
if (nums[i] > max) {
max = nums[i];
}
}
}
result[resultIndex++] = max;
}
return result;
}
/**
* 定义一个队列并实现函数max得到队列的最大值,
* 要求函数max、push_back 和 pop_front时间复杂度O(1)
*/
class MaxQueue {
Deque<Integer> queue = new ArrayDeque<>();
Deque<Integer> helper = new ArrayDeque<>();
public MaxQueue() {
}
public void push_back(int val) {
queue.addLast(val);
if (helper.isEmpty()) {
helper.addLast(val);
} else {
if (val > helper.peek()) {
helper.addLast(val);
} else {
helper.addLast(helper.peek());
}
}
}
public int pop_front() {
helper.removeFirst();
return queue.removeFirst();
}
public int getMax() {
return helper.peek();
}
}
}