[LeetCode 239] Sliding Window Maximum (hard)

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

  • 题目要求的是求每个Sliding Window中的最大值,需要考虑用一种数据结构来记录当前窗口中,最大值及以后有可能成为最大值的数的index
  • 该数据结构的First Element就是当前窗口的最大值,取的时候直接取第一个即可。同时,以后有可能成为最大值的存储在First Element的后面。 所以可以用Deque

以上面例子为例,

  1. 扫描前面两个时,Sliding Window还不完整,没有最大值,所以此时只需要向Deque加入计算得到的最大值集合。
  2. 计算得到最大值集合的规则是:
    • 如果current value >= Last in Deque,就 remove Last, 直到小于为止;然后将index of current 加入Deque; (保持第一个永远是最大的)
    • 或者如果current value < Last in Deque, 也要将index of current 加入 Deque。因为其以后可能成为最大值。
      ---比如原始 Deque[5],移动窗口后,新入的元素是3,需要把3加入 Deque,变成[5, 3],因为当5移出窗口后,3 有可能是最大值。
  3. 从第三个数开始:
    -- 首先用步骤2得到最大值集合更新Deque,然后取出First Element,其就是当前窗口的最大值,加入结果集。
    -- 再看即将被移动出窗口的那个数,是否是Deque中第一个数,如果是就要将其从Deque中移出。
class Solution {
    Deque<Integer> deque = new ArrayDeque<>();
    public int[] maxSlidingWindow(int[] nums, int k) {
        // corner case
        if (nums == null || nums.length < k || k == 0) {
            return new int[0];
        }
        // init deque with [0, k - 1]
        int[] result = new int[nums.length - k + 1];
        for (int i = 0; i < k; ++ i) {
            add(nums, i);
        }
        result[0] = nums[deque.peekFirst()];
        for (int i = k; i < nums.length; ++ i) {
            add(nums, i);
            remove(nums, i - k);
            result[i - k + 1] = nums[deque.peekFirst()];
        }
        return result;
    }
    
    private void add(int[] nums, int i) {
        while (!deque.isEmpty() && nums[i] > nums[deque.peekLast()]) {
            deque.pollLast();
        }
        deque.offerLast(i);
    }
    
    private void remove(int[] nums, int left) {
        if (!deque.isEmpty() && deque.peekFirst() == left) {
            deque.pollFirst();
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 13,151评论 0 13
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,162评论 0 10
  • 早晨揉揉惺忪的双眼醒来,轻轻的拉开窗帘,望向窗外的天空,阳光明媚秋风送爽,瞬间感觉无比的恬淡愉悦。 老婆急匆匆地起...
    陆学峰阅读 5,193评论 9 24
  • Snapseed是您日常处理相片的最佳应用程序。 通过该应用程序,您指尖轻触即可轻松愉快地对相片加以美化。 Sna...
    康查舒阅读 6,261评论 0 7
  • 有一位从事房产销售的朋友,最近跟我聊了很多。 作为从业者,他说他从未如此焦虑,如此迷茫。 经历过前两年每一套房子都...
    千年守候只为等待阅读 4,758评论 1 0