Skip to content

Instantly share code, notes, and snippets.

@kanrourou
Created January 22, 2019 01:57
Show Gist options
  • Save kanrourou/e9c5100bea28d61346c35357337ce0fd to your computer and use it in GitHub Desktop.
Save kanrourou/e9c5100bea28d61346c35357337ce0fd to your computer and use it in GitHub Desktop.
class Solution {
public:
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
int len = nums.size();
deque<int> dq;
vector<int> res;
for(int i = 0; i < len; ++i)
{
while(dq.size() && nums[dq.back()] <= nums[i])
dq.pop_back();
dq.push_back(i);
while(dq.size() && dq.front() <= i - k)
dq.pop_front();
if(i >= k - 1 && k)
res.push_back(nums[dq.front()]);
}
return res;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment