-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
32 lines (27 loc) · 802 Bytes
/
Copy pathsolution.java
File metadata and controls
32 lines (27 loc) · 802 Bytes
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
// 239. Sliding Window Maximum
// https://leetcode.com/problems/sliding-window-maximum/
// Hard | Java | Accepted 2025-10-27
// Runtime 31 ms | Memory 59.6 MB
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
int[] ans = new int[nums.length-k+1];
Deque<Integer> dq = new ArrayDeque<>();
for(int i = 0; i<nums.length; i++)
{
while(!dq.isEmpty() && dq.peekFirst() < i - k + 1)
{
dq.pollFirst();
}
while(!dq.isEmpty() && nums[dq.peekLast()] <=nums[i])
{
dq.pollLast();
}
dq.offerLast(i);
if(i >= k-1)
{
ans[i-k+1] = nums[dq.peekFirst()];
}
}
return ans;
}
}