class Solution {
public:
deque<int> dep;
void clean_queue(int idx,int i,vector<int>& nums){
if(!dep.empty()&&idx==dep.front())dep.pop_front();
while(!dep.empty()&&nums[dep.back()]<nums[i])dep.pop_back();
dep.push_back(i);
}
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
vector<int> res;
for(int i=0;i<k;i++){
clean_queue(i-k,i,nums);
}
res.push_back(nums[dep.front()]);
for(int i=k;i<nums.size();i++){
clean_queue(i-k,i,nums);
res.push_back(nums[dep.front()]);
}
return res;
}
};
leetcode239.
最新推荐文章于 2024-09-28 20:24:39 发布