public int[] maxSlidingWindow(int[] nums, int k) {
        if (nums == null || k <= 0) return new int[0];

        int[] res = new int[nums.length - k + 1]; int j = 0;
        Deque<Integer> d = new LinkedList<Integer>();
        for (int i = 0; i < nums.length; ++i) {
            while (!d.isEmpty() && nums[d.getLast()] <= nums[i]) d.removeLast();
            d.addLast(i);
            if (!d.isEmpty() && d.getFirst() < i - k + 1) d.removeFirst();
            if (i >= k - 1) res[j++] = nums[d.getFirst()];
        }
        return res;
    }