Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- vector<int> maxSlidingWindow(vector<int>& nums, int k) {
- vector<int> ans;
- deque<int> q;
- for(int i=0; i<nums.size(); i++){
- while(!q.empty() && q.front() < i-k+1)
- q.pop_front();
- while(!q.empty() && nums[q.back()] < nums[i])
- q.pop_back();
- q.push_back(i);
- if(i >= k-1)
- ans.push_back(nums[q.front()]);
- }
- return ans;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement