Leetcode 239. 滑动窗口最大值

class Solution {
public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        vector<int> ret;
        if(nums.size()==0)
            return ret;

        deque<int> q;

        for(int i=0; i<nums.size(); ++i)
        {
            if(q.empty() || nums[q.back()] > nums[i] )
            {
                q.push_back(i);
            }
            else
            {
                q.pop_back();
                while(!q.empty() && nums[q.back()] <= nums[i] )
                {
                    q.pop_back();
                }
                q.push_back(i);
            }

            //减数逻辑
            if(i-k+1 > q.front())
            {
                q.pop_front();
            }

            if(i>=k-1)
            {
                ret.push_back(nums[q.front()]);
            }

        }
        return ret;
    }
};

原文地址:https://www.cnblogs.com/randyniu/p/9396178.html

时间: 2024-08-29 02:36:54

Leetcode 239. 滑动窗口最大值的相关文章

leetcode 239. 滑动窗口最大值(单调队列)

给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧.你只可以看到在滑动窗口内的 k 个数字.滑动窗口每次只向右移动一位. 返回滑动窗口中的最大值. 进阶: 你能在线性时间复杂度内解决此题吗? 示例: 输入: nums = [1,3,-1,-3,5,3,6,7], 和 k = 3输出: [3,3,5,5,6,7] 解释: 滑动窗口的位置 最大值--------------- -----[1 3 -1] -3 5 3 6 7 3 1 [3 -1 -3] 5 3 6

【leetcode 239. 滑动窗口最大值】解题报告

思路:滑动窗口的思想,只要是求连续子序列或者子串问题,都可用滑动窗口的思想 方法一: vector<int> maxSlidingWindow(vector<int>& nums, int k) { vector<int> res; if (nums.size()==0) return res; int i=0; deque<int> dq; for (i=0;i<nums.size();++i) { while(!dq.empty()&

滑动窗口最大值的golang实现

给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧.你只可以看到在滑动窗口 k 内的数字.滑动窗口每次只向右移动一位. 返回滑动窗口最大值 输入: nums = [1,3,-1,-3,5,3,6,7], 和 k = 3 输出: [3,3,5,5,6,7] 解释: 滑动窗口的位置 最大值 --------------- ----- [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

[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. Return the max sliding

Leetcode 239题 滑动窗口最大值(Sliding Window Maximum) Java语言求解

题目链接 https://leetcode-cn.com/problems/sliding-window-maximum/ 题目内容 给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧.你只可以看到在滑动窗口内的 k 个数字.滑动窗口每次只向右移动一位. 返回滑动窗口中的最大值. 输入: nums = [1,3,-1,-3,5,3,6,7], 和 k = 3 输出: [3,3,5,5,6,7] 解释: 滑动窗口的位置|最大值 -|- [1 3 -1] -3 5

leetcode ---双指针+滑动窗口

一:Minimum Size Subarray Sum 题目: Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead. For example, given the array [2,3,1,2,4,3] and s = 7, th

Leetcode 480.滑动窗口中位数

滑动窗口中位数 中位数是有序序列最中间的那个数.如果序列的大小是偶数,则没有最中间的数:此时中位数是最中间的两个数的平均数. 例如: [2,3,4],中位数是 3 [2,3],中位数是 (2 + 3) / 2 = 2.5 给出一个数组 nums,有一个大小为 k 的窗口从最左端滑动到最右端.窗口中有 k 个数,每次窗口移动 1 位.你的任务是找出每次窗口移动后得到的新窗口中元素的中位数,并输出由它们组成的数组. 例如: 给出 nums = [1,3,-1,-3,5,3,6,7],以及 k = 3

[LeetCode] 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.给定一个任意数组,和一个大小为w的窗口,该窗口从左到右依次滑动,得到窗口的每个状态下的最大值 1 // maxWindow.cpp : 定义控制台应用程序的入口点. 2 // 3 4 #include "stdafx.h" 5 #include <iostream> 6 #include <deque> 7 #include <vector> 8 #include <iterator> 9 10 using namespace std