Advertisement
nikunjsoni

239

Apr 16th, 2021
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.53 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     vector<int> maxSlidingWindow(vector<int>& nums, int k) {
  4.         vector<int> ans;
  5.         deque<int> q;
  6.        
  7.         for(int i=0; i<nums.size(); i++){
  8.             while(!q.empty() && q.front() < i-k+1)
  9.                 q.pop_front();
  10.  
  11.             while(!q.empty() && nums[q.back()] < nums[i])
  12.                 q.pop_back();
  13.            
  14.             q.push_back(i);
  15.    
  16.             if(i >= k-1)
  17.                 ans.push_back(nums[q.front()]);
  18.         }
  19.        
  20.         return ans;
  21.     }
  22. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement