资讯专栏INFORMATION COLUMN

[LeetCode] 239. Sliding Window Maximum

lentoo / 449人阅读

摘要:丢弃队首那些超出窗口长度的元素队首的元素都是比后来加入元素大的元素,所以存储的对应的元素是从小到大

Problem

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 using max heap O(nlogn)
class Solution {
    public int[] maxSlidingWindow(int[] nums, int k) {
        PriorityQueue queue = new PriorityQueue<>((i1, i2)->i2-i1);
        if (nums == null || k == 0 || nums.length < k) return new int[0];
        int[] res = new int[nums.length-k+1];
        for (int i = 0; i < k; i++) queue.offer(nums[i]);
        res[0] = queue.peek();
        for (int i = k; i < nums.length; i++) {
            queue.remove(nums[i-k]);
            queue.offer(nums[i]);
            res[i-k+1] = queue.peek();
        }
        return res;
    }
}
using Deque to maintain a window O(n)
class Solution {
    public int[] maxSlidingWindow(int[] nums, int k) {
        if (nums == null || k == 0 || nums.length < k) return new int[0];
        int[] res = new int[nums.length-k+1];
        int index = 0;
        Deque queue = new ArrayDeque<>(); //queue to save index
        for (int i = 0; i < nums.length; i++) {
            //丢弃队首那些超出窗口长度的元素 index < i-k+1
            if (!queue.isEmpty() && queue.peek() < i-k+1) queue.poll();
            //队首的元素都是比后来加入元素大的元素,所以存储的index对应的元素是从小到大
            while (!queue.isEmpty() && nums[queue.peekLast()] < nums[i]) queue.pollLast();
            queue.offer(i);
            if (i >= k-1) res[index++] = nums[queue.peek()];
        }
        return res;
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/72277.html

相关文章

  • leetcode239. Sliding Window Maximum

    摘要:题目要求假设有一个数组和一个长度为的窗口,数组长度。当窗口右滑时,会删除下标上的值,并加入下标上的值。此时中记录的值编程了,并返回当前的最大值为。一旦最大值失效,就从窗口中重新找一个最大值就好了。 题目要求 Given an array nums, there is a sliding window of size k which is moving from the very lef...

    sPeng 评论0 收藏0
  • LeetCode 之 JavaScript 解答第239题 —— 滑动窗口最大值(Sliding W

    摘要:你只可以看到在滑动窗口内的数字。滑动窗口每次只向右移动一位。返回滑动窗口最大值。算法思路暴力破解法用两个指针,分别指向窗口的起始位置和终止位置,然后遍历窗口中的数据,求出最大值向前移动两个指针,然后操作,直到遍历数据完成位置。 Time:2019/4/16Title: Sliding Window MaximumDifficulty: DifficultyAuthor: 小鹿 题目...

    spacewander 评论0 收藏0
  • [LintCode/LeetCode] Sliding Window Maximum/Median

    摘要:窗口前进,删队首元素保证队列降序加入当前元素下标从开始,每一次循环都将队首元素加入结果数组 Sliding Window Maximum Problem Given an array of n integer with duplicate number, and a moving window(size k), move the window at each iteration fro...

    crelaber 评论0 收藏0
  • [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...

    lvzishen 评论0 收藏0
  • Sliding Window Maximum

    摘要:题目链接这道题用,注意一下存的是,因为要判断是否到最大的值,是通过现在的和第一个的差来判断的。 Sliding Window Maximum 题目链接:https://leetcode.com/problems... 这道题用deque,注意一下存的是index,因为要判断是否到最大的window值,是通过现在的index和deque第一个index的差来判断的。 public cla...

    mrcode 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<