-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSlidingWindows.cpp
More file actions
45 lines (35 loc) · 944 Bytes
/
Copy pathSlidingWindows.cpp
File metadata and controls
45 lines (35 loc) · 944 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <queue>
#include <iostream>
using namespace std;
//生成窗口最大值数组
//{4,3,5,4,3,3,6,7} -> {5,5,5,4,6,7}
//滑动窗口来解决
//w表示窗口大小, w > 0 && w <= nums.size()
vector<int> pro(vector<int>& nums,int w)
{
int n = nums.size();
vector<int> res(n - w + 1);
//存储的是下标
deque<int> SlicdingQueue;
int index = 0;
for(int i = 0;i < n;i++)
{
while(!SlicdingQueue.empty() && nums[SlicdingQueue.back()] <= nums[i])
SlicdingQueue.pop_back();
//合法情况放入
SlicdingQueue.push_back(i);
if(SlicdingQueue.front() == i - w)
SlicdingQueue.pop_front();
if(i - w >= -1)
res[index++] = nums[SlicdingQueue.front()];
}
return res;
}
int main()
{
vector<int> nums = {4,3,5,4,3,3,6,7};
vector<int> res = pro(nums,3);
for(auto& it : res)
cout << it << endl;
return 0;
}