knakul853

Untitled

Jun 18th, 2020
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. /**
  2. knakul853
  3. **/
  4. class Solution {
  5. public:
  6.     vector<int> maxSlidingWindow(vector<int>& nums, int k) {
  7.        
  8.         int n = (int)nums.size();
  9.         deque<int>dq;
  10.         vector<int>ans;
  11.         int i;
  12.        
  13.         for(i=0;i<n;i++)
  14.         {
  15.             if( !dq.empty() && dq.front() == i-k )dq.pop_front();
  16.            
  17.             while( !dq.empty() && nums[dq.back()] < nums[i] ){ dq.pop_back();}
  18.            
  19.             dq.push_back(i);
  20.            
  21.             if(i >= k-1){
  22.                 ans.push_back(nums[dq.front()]);
  23.             }
  24.         }
  25.        
  26.        
  27.         return ans;
  28.        
  29.     }
  30. };
Add Comment
Please, Sign In to add comment