-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path65.滑动窗口的最大值.cpp
More file actions
28 lines (28 loc) · 985 Bytes
/
65.滑动窗口的最大值.cpp
File metadata and controls
28 lines (28 loc) · 985 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution {
public:
vector<int> maxInWindows(const vector<int>& num, unsigned int size)
{
vector<int> res;
deque<int> index;
if(num.size() >= size && size > 0){
//处理第一个滑窗
for(int i = 0; i < size; ++i){
while(!index.empty() && num[i] >= num[index.back()])
index.pop_back();
index.push_back(i);
}
res.push_back(num[index.front()]);
//处理后续的滑窗
for(int i = size; i < num.size(); ++i){
while(!index.empty() && num[i] > num[index.back()])
index.pop_back();
//若该元素已经从窗中滑出,则应该删除
if(!index.empty() && index.front() <= i - size)
index.pop_front();
index.push_back(i);
res.push_back(num[index.front()]);
}
}
return res;
}
};