-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxWindows.java
More file actions
28 lines (26 loc) · 797 Bytes
/
MaxWindows.java
File metadata and controls
28 lines (26 loc) · 797 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
package SlidingWindow;
import java.util.ArrayList;
import java.util.List;
/*
Given an array of numbers and a window size.
Return the max value for every window in the number array. (sliding window)
*/
// Time Complexity: O(n)
// Space: O(n)
public class MaxWindows {
public static Integer [] maxWindows (int[] vals, int window){
List<Integer> results = new ArrayList<>();
int max =0;
for (int i =0; i<Math.min(window,vals.length);i++){
max = Math.max(max,vals[i]);
}
results.add(max);
if(vals.length>window){
for(int i = window-1;i<vals.length-1;i++){
max=Math.max(max,vals[i+1]);
results.add(max);
}
}
return results.toArray(Integer[]::new);
}
}