[Leetcode] Sliding Window Maximum 滑动窗口最大值

Sliding Window Maximum

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.数组

For example, Given nums = [1,3,-1,-3,5,3,6,7], and k = 3.ide

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

Therefore, return the max sliding window as [3,3,5,5,6,7].code

Note: You may assume k is always valid, ie: 1 ≤ k ≤ input array's size for non-empty array.队列

Follow up: Could you solve it in linear time?element

Hint:rem

How about using a data structure such as deque (double-ended queue)? The queue size need not be the same as the window’s size. Remove redundant elements and the queue should store only elements that need to be considered.input

优先队列

复杂度

时间 O(NlogK) 空间 O(K)it

思路

维护一个大小为K的最大堆,依此维护一个大小为K的窗口,每次读入一个新数,都把堆中窗口最左边的数扔掉,再把新数加入堆中,这样堆顶就是这个窗口内最大的值。io

注意

-结果数组的大小是nums.length + 1 - k, 赋值时下标也是i + 1 - kast

代码

public class Solution {
    public int[] maxSlidingWindow(int[] nums, int k) {
        if(nums == null || nums.length == 0) return new int[0];
        PriorityQueue<Integer> pq = new PriorityQueue<Integer>(Collections.reverseOrder());
        int[] res = new int[nums.length + 1 - k];
        for(int i = 0; i < nums.length; i++){
            // 把窗口最左边的数去掉
            if(i >= k) pq.remove(nums[i - k]);
            // 把新的数加入窗口的堆中
            pq.offer(nums[i]);
            // 堆顶就是窗口的最大值
            if(i + 1 >= k) res[i + 1 - k] = pq.peek();
        }
        return res;
    }
}

双向队列

复杂度

时间 O(N) 空间 O(K)

思路

咱们用双向队列能够在O(N)时间内解决这题。当咱们遇到新的数时,将新的数和双向队列的末尾比较,若是末尾比新数小,则把末尾扔掉,直到该队列的末尾比新数大或者队列为空的时候才住手。这样,咱们能够保证队列里的元素是从头至尾降序的,因为队列里只有窗口内的数,因此他们其实就是窗口内第一大,第二大,第三大...的数。保持队列里只有窗口内数的方法和上个解法同样,也是每来一个新的把窗口最左边的扔掉,而后把新的加进去。然而因为咱们在加新数的时候,已经把不少没用的数给扔了,这样队列头部的数并不必定是窗口最左边的数。这里的技巧是,咱们队列中存的是那个数在原数组中的下标,这样咱们既能够直到这个数的值,也能够知道该数是否是窗口最左边的数。这里为何时间复杂度是O(N)呢?由于每一个数只可能被操做最多两次,一次是加入队列的时候,一次是由于有别的更大数在后面,因此被扔掉,或者由于出了窗口而被扔掉。

代码

public class Solution {
    public int[] maxSlidingWindow(int[] nums, int k) {
        if(nums == null || nums.length == 0) return new int[0];
        LinkedList<Integer> deque = new LinkedList<Integer>();
        int[] res = new int[nums.length + 1 - k];
        for(int i = 0; i < nums.length; i++){
            // 每当新数进来时,若是发现队列头部的数的下标,是窗口最左边数的下标,则扔掉
            if(!deque.isEmpty() && deque.peekFirst() == i - k) deque.poll();
            // 把队列尾部全部比新数小的都扔掉,保证队列是降序的
            while(!deque.isEmpty() && nums[deque.peekLast()] < nums[i]) deque.removeLast();
            // 加入新数
            deque.offerLast(i);
            // 队列头部就是该窗口内第一大的
            if((i + 1) >= k) res[i + 1 - k] = nums[deque.peek()];
        }
        return res;
    }
}
相关文章
相关标签/搜索