-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathKSizeSubarrayMax.java
More file actions
43 lines (35 loc) · 1.54 KB
/
KSizeSubarrayMax.java
File metadata and controls
43 lines (35 loc) · 1.54 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
import java.util.*;
class Solution {
// Function to find maximum of each subarray of size k.
public ArrayList<Integer> max_of_subarrays(int k, int arr[]) {
ArrayList<Integer> result = new ArrayList<>();
Deque<Integer> deque = new LinkedList<>();
// Process the first k elements of the array
for (int i = 0; i < k; i++) {
// Remove elements smaller than the current element from the deque
while (!deque.isEmpty() && arr[deque.peekLast()] <= arr[i]) {
deque.removeLast();
}
// Add the current element at the end of the deque
deque.addLast(i);
}
// Process the rest of the elements
for (int i = k; i < arr.length; i++) {
// The front of the deque is the largest element of the previous window
result.add(arr[deque.peekFirst()]);
// Remove the elements that are out of this window
while (!deque.isEmpty() && deque.peekFirst() <= i - k) {
deque.removeFirst();
}
// Remove elements smaller than the current element from the deque
while (!deque.isEmpty() && arr[deque.peekLast()] <= arr[i]) {
deque.removeLast();
}
// Add the current element at the end of the deque
deque.addLast(i);
}
// Add the maximum of the last window
result.add(arr[deque.peekFirst()]);
return result;
}
}