【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()&&nums[i]>nums[dq.back()]) //在尾部添加元素,并保证左边元素都比尾部大
                dq.pop_back();
            dq.push_back(i);
            if (i-k==dq.front())    //在头部移除元素
                dq.pop_front();
            if (i>=k-1)
                res.push_back(nums[dq.front()]);    // 存放每次窗口内的最大值
        }
        return res;
    }

原文地址:https://www.cnblogs.com/brianyi/p/10804395.html

时间: 2024-10-07 15:12:39

【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. 滑动窗口最大值

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

LeetCode: Pascal&#39;s Triangle II 解题报告

Pascal's Triangle II Total Accepted: 19384 Total Submissions: 63446 My Submissions Question SolutionGiven an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to us

滑动窗口最大值的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】Word Search II 解题报告

[题目] Given a 2D board and a list of words from the dictionary, find all words in the board. Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The sa

【LeetCode】Course Schedule II 解题报告

[题目] There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1] Given the total number of courses an

【LeetCode】Jump Game II 解题报告

[题目] Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of