-
Notifications
You must be signed in to change notification settings - Fork 72
/
Solution.java
34 lines (31 loc) · 1012 Bytes
/
Solution.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
package g0201_0300.s0239_sliding_window_maximum;
// #Hard #Top_100_Liked_Questions #Array #Heap_Priority_Queue #Sliding_Window #Queue
// #Monotonic_Queue #Udemy_Arrays #Big_O_Time_O(n*k)_Space_O(n+k)
// #2024_11_16_Time_26_ms_(95.89%)_Space_59.6_MB_(38.70%)
import java.util.LinkedList;
public class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
int n = nums.length;
int[] res = new int[n - k + 1];
int x = 0;
LinkedList<Integer> dq = new LinkedList<>();
int i = 0;
int j = 0;
while (j < nums.length) {
while (!dq.isEmpty() && dq.peekLast() < nums[j]) {
dq.pollLast();
}
dq.addLast(nums[j]);
if (j - i + 1 == k) {
res[x] = dq.peekFirst();
++x;
if (dq.peekFirst() == nums[i]) {
dq.pollFirst();
}
++i;
}
++j;
}
return res;
}
}