【LeetCode 239】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.

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].

题意:

  给定一个数组序列,以及一个固定大小的窗口K,用k从前到后扫描一次数组,返回窗口每个状态下的最大值。

思路:

  使用双端队列deque当做滑动窗口,保证每个状态下最大值总是在队首。比如[1,3,-1,-3,5,3,6,7],那么deque的状态分别为[1] 、[3] (前2步,填装滑动窗口) ,(从第三步正式开始)[3,-1]、 [3,-1,-3]、[5]、[5,3]、[6]、[7],可以看到每次队列的队首元素都是当前滑动窗口的最大值,具体过程可参考代码,有注释。时间复杂度O(n)。

C++:

class Solution {
public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {

        vector<int> ret;
        if(nums.size() == 0)
            return ret;

        //双端队列,存储的是元素的下标,不是元素的值
        deque<int> slideWindow;
        for(int i = 0; i < k; i++)
        {
            //如果要进队列的数比它前面的数字大,则将它前面的数字从后面删除,直到它前边的数字比它大或者队列为空
            while(!slideWindow.empty() && nums[i] >= nums[slideWindow.back()])
            {
                slideWindow.pop_back();
            }

            //加入新元素到队列
            slideWindow.push_back(i);
        }

        for(int i = k; i < nums.size(); i++)
        {
            //队首元素即为当前窗口最大值
            ret.push_back(nums[slideWindow.front()]);

            //如果要进队列的数比它前面的数字大,则将它前面的数字从后面删除,直到它前边的数字比它大或者队列为空
            while(!slideWindow.empty() && nums[i] >= nums[slideWindow.back()])
            {
                slideWindow.pop_back();
            }

            //如果当前队首的元素已经不在窗口内部,则将其从队列前边删除
            if(!slideWindow.empty() && slideWindow.front() <= i - k)
                slideWindow.pop_front();

            //加入新元素到队列
            slideWindow.push_back(i);
        }

        //不要忘了最后一个窗口的结果 - -
        ret.push_back(nums[slideWindow.front()]);

        return ret;
    }
};
时间: 2024-12-11 12:37:40

【LeetCode 239】Sliding Window Maximum的相关文章

【Leetcode】Sliding Window Maximum

题目链接:https://leetcode.com/problems/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 sli

【原创】Sliding Window Maximum 解法分析

这道题是lintcode上的一道题,当然leetcode上同样有. 本题需要寻找O(N)复杂度的算法. 解体思路比较有特点,所以容易想到参考 最小栈 的解题办法. 但是最小栈用栈维护最小值很直观,这道题是队列,用什么数据结构好呢?也许看完暴力解会有点启发. 但是思路还是一样的,最大的要在最前面(直接获取结果),小的值在后面保留下来(防止之后遍历到的时候丢失数据).并且某值出窗口的时候需要判断是否要修改排在最前面的值. 一.暴力解 当然直观看,暴力求解是 O(NK)的复杂度,大体的代码如下:(写的

【LeetCode】239. 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.

leetcode 239. Sliding Window Maximum

239. 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 posi

[LeetCode][JavaScript]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.

【LeetCode 229】Majority Element II

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space. 思路: [LeetCode 169]Majority Element 的拓展,这回要求的是出现次数超过三分之一次的数字咯,动动我们的大脑思考下,这样的数最多会存在几个呢,当然是2个嘛.因此,接着上一题的方

【LeetCode OJ】Sum Root to Leaf Numbers

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 # Definition for a  binary tree node # class TreeNode: #     def __init__(self, x): #         self.val = x #         self.left = No

【LeetCode OJ】Longest Consecutive Sequence

Problem Link: http://oj.leetcode.com/problems/longest-consecutive-sequence/ This problem is a classical problem where we can reduce the running time by the help of hash table. By given a list of numbers, we can find the longest consecutive sequence b

【LeetCode OJ】Word Ladder I

Problem Link: http://oj.leetcode.com/problems/word-ladder/ Two typical techniques are inspected in this problem: Hash Table. One hash set is the words dictionary where we can check if a word is in the dictionary in O(1) time. The other hash set is us