当前位置:网站首页>Leetcode: offer 59 - I. maximum value of sliding window
Leetcode: offer 59 - I. maximum value of sliding window
2022-07-01 03:32:00 【Re:fused】
subject : The finger of the sword Offer 59 - I. Maximum sliding window
The question :
Given an array nums And the size of the sliding window k, Please find the maximum value in all sliding windows .
Ideas :
Take priority queue , The team leader is a big element , Two data are stored in a limited queue , Values and positions , First, put the front k Put data into , When sliding the window , Add new elements , Check whether the biggest element of the team leader is the current window , If not, pop up , Until it is the current window .
Code :
class Solution {
public:
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
priority_queue<pair<int, int>>q;
vector<int>temp;
if(nums.size() == 0)return temp;
for(int i = 0; i < k; i++){
q.emplace(nums[i], i);
}
vector<int>ans = {
q.top().first};
for(int i = k; i < nums.size(); i++){
q.emplace(nums[i], i);
while(q.top().second <= i-k)q.pop();
ans.push_back(q.top().first);
}
return ans;
}
};
边栏推荐
- [小样本分割]论文解读Prior Guided Feature Enrichment Network for Few-Shot Segmentation
- File upload and download
- 二叉树神级遍历:Morris遍历
- Pytest -- plug-in writing
- JS daily development tips (continuous update)
- GCC usage, makefile summary
- 文件上传下载
- Chapter 03_ User and authority management
- multiple linear regression
- 倍福TwinCAT3 Ads相关错误详细列表
猜你喜欢
随机推荐
Chapter 03_ User and authority management
shell脚本使用两个横杠接收外部参数
Hal library setting STM32 interrupt
JS daily development tips (continuous update)
家居网购项目
雪崩问题以及sentinel的使用
线程数据共享和安全 -ThreadLocal
Research on target recognition and tracking based on 3D laser point cloud
How do I use Google Chrome 11's Upload Folder feature in my own code?
[nine day training] content III of the problem solution of leetcode question brushing Report
Ultimate dolls 2.0 | encapsulation of cloud native delivery
还在浪费脑细胞自学吗,这份面试笔记绝对是C站天花板
A few lines of transaction codes cost me 160000 yuan
[us match preparation] complete introduction to word editing formula
Take you through a circuit board, from design to production (dry goods)
Cookie&Session
Chapitre 03 Bar _ Gestion des utilisateurs et des droits
手把手带你了解一块电路板,从设计到制作(干货)
C语言多线程编程入门学习笔记
[daily training] 1175 Prime permutation









