239. Sliding Window Maximum
Tags: ‘Heap’, ‘Sliding Window’
Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position. Return the max sliding window.
Example:
Input: nums =[1,3,-1,-3,5,3,6,7], and k = 3 Output:[3,3,5,5,6,7] Explanation:Window position Max --------------- ----- [1 3 -1] -3 5 3 6 7 3 1 [3 -1 -3] 5 3 6 7 3 1 3 [-1 -3 5] 3 6 7 5 1 3 -1 [-3 5 3] 6 7 5 1 3 -1 -3 [5 3 6] 7 6 1 3 -1 -3 5 [3 6 7] 7
Note: 
You may assume k is always valid, 1 ≤ k ≤ input array's size for non-empty array.
Follow up:
Could you solve it in linear time?
Solution
Brute force:length of out put is n-k+1,  Time O((n-k+1)*k)  O(nk) Space O(1)
public int[] maxSlidingWindow(int[] nums, int k) {
    if (k == 0) return new int[0];
    
    int[] result = new int[nums.length - k + 1];
    for (int i = k - 1; i < nums.length; i++) {
        int max = nums[i];
        for (int j = 1; j < k; j++) { // 
            if (nums[i - j] > max) max = nums[i - j];
        }
        result[i - (k - 1)] = max;
    } 
    return result;
}
MaxHeap Solution. Since we know each time which number to add/remove, just maintain a MaxHeap of size k. Time O(nlogk) Space O(k)
class Solution {
    public int[] maxSlidingWindow(int[] nums, int k) {
        if (k == 0) return new int[0];
        
        int[] result = new int[nums.length - k + 1];
        PriorityQueue<Integer> heap = new PriorityQueue<Integer>(new Comparator<Integer>() { // Max heap
            @Override
            public int compare(Integer x, Integer y) {
                return Integer.compare(y, x);
            }
        });
        
        for (int i = 0; i < k; i++) {
            heap.add(nums[i]);
        }
        result[0] = heap.peek();
        
        for (int i = k; i < nums.length; i++) {
            heap.remove(nums[i - k]);
            heap.add(nums[i]);
            result[i - k + 1] = heap.peek();
        }
        return result;
    }
}
Solution 3, best. Using Monotonic Queue 
Time O(n) Space O(k)
class Solution {
    public int[] maxSlidingWindow(int[] nums, int k) {
        if (k == 0) return new int[0];
        
        int[] result = new int[nums.length - k + 1];
        Deque<Integer> mono = new LinkedList<>(); // storing index
        for (int i = 0; i < nums.length; i++) {
            while (mono.size() > 0 && nums[i] > mono.getLast()) {
                mono.removeLast();
            }
            mono.addLast(i);
            if (i - k + 1 >= 0) { // can be add to result
                result[i - k + 1] = nums[mono.getFirst()];
            }
            if (i - k + 1 >= mono.getFirst()) { // remove first if size reach limit k
                mono.removeFirst();
            }
        }
        return result;
    }
}