Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8afa890

Browse files
Update 0239-sliding-window-maximum.java
1 parent f31756c commit 8afa890

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

java/0239-sliding-window-maximum.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
class Solution {
2-
2+
//It also has nice explainasion.
33
public int[] maxSlidingWindow(int[] nums, int k) {
44
int[] ans = new int[nums.length - k + 1];
55
int j = 0;
66
Deque<Integer> q = new LinkedList<>();
77
for (int i = 0; i < nums.length; i++) {
8+
//here we are checking if this is in current bound if not removing it.
89
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.
911
while (!q.isEmpty() && nums[i] > nums[q.peekLast()]) q.pollLast();
1012
q.offer(i);
1113
if (i >= k - 1) ans[j++] = nums[q.peekFirst()];

0 commit comments

Comments
 (0)