Thanks to visit codestin.com Credit goes to github.com
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 f31756c commit 8afa890Copy full SHA for 8afa890
java/0239-sliding-window-maximum.java
@@ -1,11 +1,13 @@
1
class Solution {
2
-
+ //It also has nice explainasion.
3
public int[] maxSlidingWindow(int[] nums, int k) {
4
int[] ans = new int[nums.length - k + 1];
5
int j = 0;
6
Deque<Integer> q = new LinkedList<>();
7
for (int i = 0; i < nums.length; i++) {
8
+ //here we are checking if this is in current bound if not removing it.
9
if (!q.isEmpty() && q.peekFirst() < i - k + 1) q.pollFirst();
10
+ //here we are checking if current element is greater than value in queue.. then keep polling it.
11
while (!q.isEmpty() && nums[i] > nums[q.peekLast()]) q.pollLast();
12
q.offer(i);
13
if (i >= k - 1) ans[j++] = nums[q.peekFirst()];
0 commit comments