LeetCode239. Sliding Window Maximum

news/2024/5/20 8:59:15 标签: 算法, 数据结构, leetcode, c++, 滑动窗口

文章目录

    • 一、题目
    • 二、题解

一、题目

You are given an array of integers 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 1:

Input: nums = [1,3,-1,-3,5,3,6,7], 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
Example 2:

Input: nums = [1], k = 1
Output: [1]

Constraints:

1 <= nums.length <= 105
-104 <= nums[i] <= 104
1 <= k <= nums.length

二、题解

方法一:优先级队列法

class Solution {
public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        int n = nums.size();
        priority_queue<pair<int,int>> q;
        for(int i = 0;i < k;i++){
            q.emplace(nums[i],i);
        }
        vector<int> res = {q.top().first};
        for(int i = k;i < n;i++){
            q.emplace(nums[i],i);
            //若最大值不在窗口中,不断移除元素
            while(q.top().second <= i-k) q.pop();
            res.push_back(q.top().first);
        }
        return res;
    }
};

方法二、单调队列法

class Solution {
public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        int n = nums.size();
        deque<int> q;
        for(int i = 0;i < k;i++){
            while(!q.empty() && nums[i] > nums[q.back()]) q.pop_back();
            q.push_back(i);
        }
        vector<int> res = {nums[q.front()]};
        for(int i = k;i < n;i++){
            while(!q.empty() && nums[i] > nums[q.back()]) q.pop_back();
            q.push_back(i);
            while(!q.empty() && q.front() <= i - k) q.pop_front();
            res.push_back(nums[q.front()]);
        }
        return res;
    }
};

http://www.niftyadmin.cn/n/5282369.html

相关文章

1849_emacs_org-mode提取源代码

Grey 全部学习内容汇总&#xff1a; https://github.com/greyzhang/g_org 1849_emacs_org-mode提取源代码 代码提取是从 org-mode 的org文件中提取生成我们所需要的代码的过程&#xff0c;这里结合官方的文档来看看还有什么细节的配置信息。 主题由来介绍 文学式编程其实是…

python脚本 链接到ssh服务器 快速登录ssh服务器 ssh登录

此文分享一个python脚本,用于管理和快速链接到ssh服务器。 效果演示 🔥完整演示效果 👇第一步,显然,我们需要选择功能 👇第二步,确认 or 选择ssh服务器,根据配置文件中提供的ssh信息,有以下情况 👇场景一,只有一个候选ssh服务器,则脚本会提示用户是否确认链…

FFmpeg——视频处理工具安装以及简单命令学习。

FFmpeg 是一个免费、开源且高度可定制的多媒体处理工具&#xff0c;它是一个强大的跨平台框架&#xff0c;用于处理音频、视频、多媒体流和图像。FFmpeg 的主要功能包括解码、编码、转码、流处理、多路复用、分离、合并、过滤等&#xff0c;支持多种音视频格式&#xff0c;包括…

61权限提升-RedisPostgre令牌窃取进程注入

主要讲解redis数据库和postgresql数据库&#xff0c;然后还要两个windows的提权方式令牌窃取和进程注入。 postgresql是基于两个cve的漏洞&#xff0c;redis的提权方式第一种是利用任务执行的反弹shell&#xff0c;第二个是写一个ssh-keygen的公钥使用私钥登录&#xff0c;这是…

Leetcode—剑指Offer LCR 025.两数相加II【中等】

2023每日刷题&#xff08;六十七&#xff09; Leetcode—LCR 025.两数相加II 实现代码 /*** Definition for singly-linked list.* struct ListNode {* int val;* struct ListNode *next;* };*/struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode…

【HTML】使用js给input标签增加disabled属性

目录 1.常规text标签 2.radio标签 1.常规text标签 在JavaScript中&#xff0c;您可以通过修改元素的属性来给input标签增加disabled属性。这可以通过使用setAttribute方法来完成。以下是一个简单的例子&#xff1a; // 假设您的input元素的id是myInput var inputElement doc…

【Spring实战】02 配置多数据源

文章目录 1. 配置数据源信息2. 创建第一个数据源3. 创建第二个数据源4. 创建启动类及查询方法5. 启动服务6. 创建表及做数据7. 查询验证8. 详细代码总结 通过上一节的介绍&#xff0c;我们已经知道了如何使用 Spring 进行数据源的配置以及应用。在一些复杂的应用中&#xff0c;…

【C++篇】讲解deque容器的基本操作

文章目录 &#x1f354;简述deque容器&#x1f339;基本操作⭐赋值操作⭐大小操作⭐插入和删除⭐排序 &#x1f354;简述deque容器 deque&#xff08;双端队列&#xff09;是 C 标准库中的一种容器&#xff0c;它允许在两端高效地进行插入和删除操作。下面我会使用理论来解释 …