Created
January 22, 2019 01:57
-
-
Save kanrourou/e9c5100bea28d61346c35357337ce0fd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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