Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 77045e4

Browse files
sliding window maximum
1 parent a25a314 commit 77045e4

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package java_problem.slidewindow;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
public class SlidingWindowMaximum {
8+
public static void main(String args[]) {
9+
System.out.println(Arrays.toString(maxSlidingWindow(new int[]{1, 3, -1, -3, 5, 3, 6, 7}, 3)));
10+
}
11+
12+
public static int[] maxSlidingWindow(int[] nums, int k) {
13+
int r = 0;
14+
int n = nums.length;
15+
int max = Integer.MIN_VALUE;
16+
List<Integer> list = new ArrayList<>();
17+
while (r <= n - k) {
18+
for (int i = 0; i < k; i++) {
19+
max = Math.max(max, nums[r + i]);
20+
}
21+
list.add(max);
22+
r++;
23+
}
24+
return list.stream().mapToInt(i -> i).toArray();
25+
}
26+
27+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /