leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0239.cpp (538B)


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