239. 滑动窗口最大值
This commit is contained in:
22
test011.cpp
Normal file
22
test011.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
// 1.<2E><><EFBFBD>ȶ<EFBFBD><C8B6>нⷨ
|
||||
class Solution {
|
||||
public:
|
||||
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
|
||||
int n = nums.size();
|
||||
priority_queue<pair<int, int>> q;
|
||||
for(int i = 0; i < k; i++){
|
||||
q.emplace(nums[i], i);
|
||||
}
|
||||
vector<int> ans = {q.top().first};
|
||||
for(int i = k; i < n; i++){
|
||||
q.emplace(nums[i], i);
|
||||
while(q.top().second <= (i - k)){
|
||||
q.pop();
|
||||
}
|
||||
ans.push_back(q.top().first);
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
Reference in New Issue
Block a user