import java.util.*;
public class Solution {
    public ArrayList<Integer> maxInWindows(int [] num, int size) {
        ArrayList<Integer> res = new ArrayList<>();
        if (num == null || num.length < 1 || size < 1) {
            return res;
        }
        int n = num.length;
        Deque<Integer> dq = new LinkedList<>();
        for (int i = 0; i < n; i++) {
            while (!dq.isEmpty() && num[dq.peekLast()] < num[i]) {
                dq.pollLast();
            }
            dq.offerLast(i);
            if (i - dq.peekFirst() + 1 > size) {
                dq.pollFirst();
            }
            if (i + 1 >= size) {
                res.add(num[dq.peekFirst()]);
            }
        }
        return res;
    }
}