have another one easier to understand that beats 93%

public int[] maxSlidingWindow(int[] nums, int k) { if (nums.length == 0 || k == 0) { return new int[0]; } ArrayDeque<Integer> maxInds = new ArrayDeque<Integer>(); int[] results = new int[nums.length - k + 1]; for (int i=0; i<=nums.length-k; i++) { int start = i; int end = i+k-1; int curMaxInd = maxInds.peek() == null ? -1 : maxInds.peek(); if (curMaxInd >= start && curMaxInd <= end) { //compare with the new element with index end if prev max within current range, //if new element is larger, update the maxInds queue as well if (nums[end] > nums[curMaxInd]) { maxInds.removeFirst(); maxInds.addFirst(end); curMaxInd = maxInds.getFirst(); } results[i] = nums[curMaxInd]; } else { //remove maxInd out of range, recalculate the max. //We can just remove since we check always check array from left to right, //next iteration index will always be larger maxInds.pollFirst(); //return null if empty, don't want exception from maxInds is empty int max = 0; int maxInd = start; for (int j=start; j<=end; j++) { if (nums[j] > max) { max = nums[j]; maxInd = j; } } maxInds.addFirst(maxInd); results[i] = nums[maxInd]; } } return results; }Sliding Window Maximum