当前位置:网站首页>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;
}
};
边栏推荐
猜你喜欢

Hello World generation

Let's just say I can use thousands of expression packs

Ctfshow blasting WP

Elk elegant management server log

Stop saying that you can't solve the "cross domain" problem

监听器 Listener

终极套娃 2.0 | 云原生交付的封装

pytorch nn.AdaptiveAvgPool2d(1)

FCN full Convolution Network Understanding and Code Implementation (from pytorch Official Implementation)

Feature pyramid networks for object detection
随机推荐
Research on target recognition and tracking based on 3D laser point cloud
Edge drawing: a combined real-time edge and segment detector
Leetcode:剑指 Offer 59 - I. 滑动窗口的最大值
Best used trust automation script (shell)
go实现命令行的工具cli
LeetCode 144二叉树的前序遍历、LeetCode 114二叉树展开为链表
Analyze datahub, a new generation metadata platform of 4.7K star
力扣-两数之和
Cookie&Session
GCC usage, makefile summary
实现pow(x,n)函数
Learning notes for introduction to C language multithreaded programming
Cookie&Session
Redis tutorial
Force buckle - sum of two numbers
手把手带你了解一块电路板,从设计到制作(干货)
ES6解构语法详解
网页不能右键 F12 查看源代码解决方案
Druid监控统计数据源
gcc使用、Makefile总结