LeetCode 239. Sliding Window Maximum

本文介绍了一种求解滑动窗口最大值的问题,并提供了两种解决方案。第一种方法通过逐个比较窗口内的元素来确定最大值,而第二种方法则利用双端队列(deque)进行优化,实现线性时间复杂度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.
题意:给你一个数组nums,有一个大小为k的滑动窗口从数组的最左移动到最右。你只能看到窗口里的k个数。窗口每次往右边滑动一个位置。

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].
Note:
You may assume k is always valid, ie: 1 ≤ k ≤ input array’s size for non-empty array.
注意:假设k总是有效,如 1 ≤ k ≤ 非空数组的大小

Follow up:
Could you solve it in linear time?
另外:你能否在线性时间内解决这个问题?

开始的解题思路,从左滑到右,记录窗口的最大值和最大值的索引:
1. 如果窗口滑动后,该最大值依然在窗口内,且大于窗口最右边的数,则该最大值依然是窗口的最大值。
2. 否则,重新在窗口内寻找最大值,并记录最大值的索引。

class Solution {
public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        if(k == 1 || nums.empty()) return nums;

        int maxi = nums[0], iMax = 0;   // maxi 记录当前窗口最大数,iMax记录最大数的索引
        int l = 0, r = k - 1, n = nums.size() - 1;  // 窗口左右指针
        vector<int> res;             
        findMax(nums, l, r, maxi, iMax);    // 找到第一个窗口的最大数和其索引
        res.push_back(maxi);
        while(r < n){
            ++l; ++r;
            if(maxi > nums[r] && iMax >= l){    // 移动窗口后,原来最大的数大于窗口最右边的数,且原来最大的数在窗口内
                res.push_back(maxi);
                continue;
            }
            else{       // 原来最大的数不在窗口内
                findMax(nums, l , r, maxi, iMax);   // 重新找到最大数
                res.push_back(maxi);
            }
        }

        return res;
    }

private:
    // 找到[l, r] 范围内的最大数和其索引
    void findMax(vector<int>& nums, const int& l, const int& r, int& maxi, int& iMax){
        if(r >= nums.size()) return;

        maxi = nums[l], iMax = l;
        for(int i = l + 1; i <= r; ++i){
            if(nums[i] > maxi){
                maxi = nums[i];
                iMax = i;
            }
        }
    }
};

分析时间复杂度和空间复杂度:
Time Complexity:
最坏情况:当数组降序的时候,会发生最坏情况,每次都需要重新寻找最大值和索引,时间复杂度为O(n * k)
最好情况:[i - ( k - 1), i + k]中的最大值在i处,所以将数组分成 n / 2k 份,每份重新查找时间为k,时间复杂度为O( n / 2k * k) = O(n)
Space Complexity:
没有额外使用空间O(1)
这里写图片描述
参考讨论中的另一种方法,使用deque:
https://leetcode.com/problems/sliding-window-maximum/discuss/65884/Java-O(n)-solution-using-deque-with-explanation

class Solution {
public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) 
    {
        std::deque<int> dq;
        std::vector<int> result;

        for(int i = 0; i < nums.size(); ++i) {
            if(!dq.empty() && dq.front() == i - k) {
                dq.pop_front();
            }

            while(!dq.empty() && nums[dq.back()] < nums[i]) 
                dq.pop_back();

            dq.push_back(i);

            if(i >= k - 1) {
                result.push_back(nums[dq.front()]);
            }
        }
        return result;
    }
};

Time Complexity:
对nums中的每一个数进出队列操作各一次,时间复杂度为O(2*n) = O(n)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值