239. Sliding Window Maximum
Contents
https://leetcode.com/problems/sliding-window-maximum/
https://leetcode.com/problems/sliding-window-maximum/submissions/1022554836/
class Solution {
public:
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
priority_queue<pair<int, int>, vector<pair<int,int>>, less<pair<int,int>>> q;
for(int i = 0; i < k; ++i) {
q.push(pair(nums[i],i));
}
vector<int> ans;
ans.push_back(q.top().first);
for(int i = k; i < nums.size(); ++i) {
q.push(pair(nums[i], i));
auto t = q.top();
while(t.second <= i - k) {
q.pop();
t = q.top();
}
ans.push_back(q.top().first);
}
return ans;
}
};