LeetCode:239. 滑动窗口最大值

class MonotonicQueue{

deque<int>maxq;

public:

void push(int n){

//将小于n的元素全部删除

while(!maxq.empty()&&maxq.back()<n){

maxq.pop_back();

}

maxq.push_back(n);

}

int max(){

return maxq.front();

}

void pop(int n){

if(n==maxq.front()){

maxq.pop_front();

}

}

};

class Solution {

public:

vector<int> maxSlidingWindow(vector<int>& nums, int k) {

MonotonicQueue window;

vector<int>res;

for(int i=0;i<nums.size();i++){

if(i<k-1){

window.push(nums[i]);

}

else{

window.push(nums[i]);

res.push_back(window.max());

window.pop(nums[i-k+1]);

}

}

return res;

}

};

相关推荐
NiKo_W1 天前
Linux 线程控制
linux·数据结构·内核·线程·进程·线程控制
PyHaVolask1 天前
数据结构与算法分析
数据结构·算法·图论
小王C语言1 天前
封装红黑树实现mymap和myset
linux·服务器·算法
幸运小圣1 天前
Set数据结构【ES6】
javascript·数据结构·es6
大佬,救命!!!1 天前
算法实现迭代2_堆排序
数据结构·python·算法·学习笔记·堆排序
Dream it possible!1 天前
LeetCode 面试经典 150_栈_简化路径(53_71_C++_中等)(栈+stringstream)
c++·leetcode·面试·
爱和冰阔落1 天前
【C++继承下】继承与友元 / static 菱形继承与虚继承 组合的详解分析
c++·面试·腾讯云ai代码助手
程序员阿鹏1 天前
49.字母异位词分组
java·开发语言·leetcode
天桥下的卖艺者1 天前
R语言手搓一个计算生存分析C指数(C-index)的函数算法
c语言·算法·r语言
Espresso Macchiato1 天前
Leetcode 3715. Sum of Perfect Square Ancestors
算法·leetcode·职场和发展·leetcode hard·树的遍历·leetcode 3715·leetcode周赛471