239. 滑动窗口最大值

This commit is contained in:
2025-09-12 23:02:11 +08:00
parent 124b24c361
commit 0740d30bdc

22
test011.cpp Normal file
View 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;
}
};
//