当前位置:网站首页>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;
}
};
边栏推荐
- Gorilla/mux framework (RK boot): RPC error code design
- FCN full Convolution Network Understanding and Code Implementation (from pytorch Official Implementation)
- Introduction to core functions of webrtc -- an article on understanding SDP PlanB unifiedplan (migrating from PlanB to unifiedplan)
- 数据交换 JSON
- EtherCAT简介
- pytest-fixture
- torch.histc
- 服务器渲染技术jsp
- Analyze datahub, a new generation metadata platform of 4.7K star
- 过滤器 Filter
猜你喜欢
随机推荐
不用加减乘除实现加法
About the application of MySQL
FCN全卷积网络理解及代码实现(来自pytorch官方实现)
过滤器 Filter
Druid monitoring statistics source
网页不能右键 F12 查看源代码解决方案
还在浪费脑细胞自学吗,这份面试笔记绝对是C站天花板
go实现命令行的工具cli
TEC: Knowledge Graph Embedding with Triple Context
multiple linear regression
Basic concepts of database
EtherCAT简介
Ouc2021 autumn - Software Engineering - end of term (recall version)
BluePrism注册下载并安装-RPA第一章
pytorch训练深度学习网络设置cuda指定的GPU可见
串口接收数据方案设计
Redis efficient like and cancel function
How to achieve 0 error (s) and 0 warning (s) in keil5
Hal library setting STM32 interrupt
The shell script uses two bars to receive external parameters







![[us match preparation] complete introduction to word editing formula](/img/e4/5ef19d52cc4ece518e79bf10667ef4.jpg)
