class Solution {
public int[] maxInWindows(int[] nums, int k) {
int n = nums.length;
int[] res = new int[n - k + 1];
int[] q = new int[n];
int hh = 0, tt = -1,j = 0;
for(int i = 0; i < n; i ++){
if(hh <= tt && i - q[hh] == k) hh ++;
while(hh <= tt && nums[i] > nums[q[tt]]) tt --;
q[++ tt] = i;
if(i - k + 1 >= 0) res[j ++] = nums[q[hh]];
}
return res;
}
}