We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 51781df commit 6e1c59eCopy full SHA for 6e1c59e
solution/0239.Sliding Window Maximum/Solution.java
@@ -0,0 +1,23 @@
1
+class Solution {
2
+ public int[] maxSlidingWindow(int[] nums, int k) {
3
+ if (nums == null || nums.length == 0 || k <= 0) {
4
+ return new int[0];
5
+ }
6
+ int[] res = new int[nums.length - k + 1];
7
+ int index = 0;
8
+ Deque<Integer> q = new ArrayDeque<>(k);
9
+ for (int i = 0; i < nums.length; ++i) {
10
+ while (!q.isEmpty() && nums[i] >= nums[q.peekLast()]) {
11
+ q.pollLast();
12
13
+ q.offerLast(i);
14
+ if (i - q.peekFirst() >= k) {
15
+ q.pollFirst();
16
17
+ if (i >= k - 1) {
18
+ res[index++] = nums[q.peekFirst()];
19
20
21
+ return res;
22
23
+}
0 commit comments