leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0239.cpp (538B)
0 class Solution { 1 public: 2 vector<int> maxSlidingWindow(vector<int> &nums, int k) { 3 int n = nums.size(); 4 vector<int> res; 5 deque<int> q; 6 7 res.reserve(n - k + 1); 8 for (int i = 0; i < n; i++) { 9 while (!q.empty() && q.front() < i - k + 1) 10 q.pop_front(); 11 while (!q.empty() && nums[q.back()] < nums[i]) 12 q.pop_back(); 13 q.push_back(i); 14 if (i >= k - 1) res.push_back(nums[q.front()]); 15 } 16 return res; 17 } 18 };