当前位置:网站首页>力扣239. 滑动窗口最大值
力扣239. 滑动窗口最大值
2022-06-21 16:53:00 【SS_zico】
给你一个整数数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的 k 个数字。滑动窗口每次只向右移动一位。
返回滑动窗口中的最大值。
示例 1:
输入: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 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
示例 2:
输入:nums = [1], k = 1
输出:[1]
示例 3:
输入:nums = [1,-1], k = 1
输出:[1,-1]
示例 4:
优先队列
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> ans = {
q.top().first};
for (int i = k; i < n; ++i) {
q.emplace(nums[i], i);
while (q.top().second <= i - k) {
q.pop();
}
ans.push_back(q.top().first);
}
return ans;
}
};
边栏推荐
- Encryption crash, is there a future for Web3 games? In depth discussion of 5 papers
- How to create your own AI creativity?
- Node的json解析
- 剑指offer 58.对称的二叉树
- C language DLL Dynamic Link Library
- AttributeError: module ‘cv2‘ has no attribute ‘gapi_ wip_ gst_ GStreamerPipeline‘
- Hain's law and Feynman's learning method
- 有哪些好用的工作汇报工具
- Development of digital collection system and construction of NFT artwork trading platform
- Cann training camp Season 2 - the opening ceremony | it starts at 7:30 tonight on time. You can't miss it!
猜你喜欢

EtherCAT object dictionary analysis

主动学习(Active Learning) 概述、策略和不确定性度量

带你区分几种并行

研发仅占3%的蕉下,是防晒黑科技,还是夏天的智商税?
![[pwn基础]Pwntools学习](/img/67/6fb8a9628d5b750b0396f083aaeb91.png)
[pwn基础]Pwntools学习

Typescript的通用类型检查

堆栈认知——栈溢出实例(ret2libc)

智慧三农数字王宁:适合初学者的前5日交易秘诀

ByteDance proposes a lightweight and efficient new network mocovit, which has better performance than GhostNet and mobilenetv3 in CV tasks such as classification and detection!

天天在都在谈的S3协议到底是什么?一文带你了解S3背后的故事
随机推荐
EtherCAT igh master station controls three Delta asdaa2 servo rotating circles
EtherCAT igh主站控制3个台达asdaa2伺服转圈圈
Mysql常见面试题
TypeScript编译生成文件对比
postman关联,完成接口自动化测试
TypeScript的类型检查
剑指offer 58.对称的二叉树
I got a pay cut in disguise
力扣160. 相交链表
nest.js实战之模块依赖传递性
How can multidimensional analysis pre summary work?
Vue. js+Node. JS full stack development tutorial: connecting to MySQL
Threejs实现波动热点效果,波动标记,波动标签显示
智慧三农数字王宁:适合初学者的前5日交易秘诀
大型网站技术架构 | 应用服务器安全防御
EtherCAT igh master station controls Esther servo to return to zero
POSIX信号量
SQL操作:WITH表达式及其应用
aws elastic beanstalk入门之简介
Xlrd finds the row of the specified content and its content