其他分享
首页 > 其他分享> > day27

day27

作者:互联网

1.剑指 Offer 59 - I. 滑动窗口的最大值

 单调队列

 1 class Solution {
 2 public:
 3     vector<int> maxSlidingWindow(vector<int>& nums, int k) {
 4       vector<int> res;
 5       deque<int> q;
 6       int n = nums.size();
 7       for(int i = 0;i < n;i ++){
 8           while(!q.empty() && q.front() <= i - k)  q.pop_front();
 9           while(!q.empty() && nums[q.back()] < nums[i])  q.pop_back();
10           q.push_back(i);
11           if(q.back() >= k - 1)
12            res.push_back(nums[q.front()]);
13       }
14       return res;
15     }
16 };

 

标签:nums,int,res,day27,back,vector,front
来源: https://www.cnblogs.com/balabalabubalabala/p/16511694.html