class Solution {
public int[] maxInWindows(int[] nums, int k) {
int n = nums.length;
int q[] = new int[n + 10];
int h = 0, t = -1;
int ans[] = new int[n - k + 1];
int idx = 0;
for(int i = 0; i < n; i++ )
{
if (h <= t && i - k + 1 > q[h]) h++;
while (h <= t && nums[q[t]] <= nums[i]) t--;
q[++t] = i;
if (i >= k - 1) ans[idx++] = nums[q[h]];
}
return ans;
}
}