class Solution {
private:
class MyQueue {
public:
deque<int> que;
void pop(const int& pt) {
if(!que.empty() && que.front() == pt) que.pop_front();
}
void push(const int& pt) {
while(!que.empty() && que.back() < pt) que.pop_back();
que.push_back(pt);
}
int getMax() {
return que.front();
}
};
public:
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
MyQueue que;
vector<int> result;
for(int i = 0; i < k; i++) {
que.push(nums[i]);
}
result.push_back(que.getMax());
for(int slow = 0, fast = k; fast < nums.size(); slow++, fast++) {
que.pop(nums[slow]);
que.push(nums[fast]);
result.push_back(que.getMax());
}
return result;
}
};
力扣239. 滑动窗口最大值
于 2025-02-13 15:14:57 首次发布