-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxInSlideWindow.cpp
More file actions
46 lines (29 loc) · 786 Bytes
/
maxInSlideWindow.cpp
File metadata and controls
46 lines (29 loc) · 786 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
45
46
#include <iostream>
#include <vector>
#include <deque>
using namespace std;
int getMaxValInWindow( const vector<int> &vec, const int &size) {
int ans;
deque<int> _slide;
for(int i = 0; i != vec.size(); ++i) {
while(!_slide.empty() && vec[i] > vec[_slide.back()] ) {
_slide.pop_back();
}
_slide.push_back(i);
if( i-size == _slide.front() ) {
_slide.pop_front();
}
if( i < 2 ) continue;
for( auto iter = _slide.begin(); iter != _slide.end(); ++iter) {
cout << vec[*iter] << " ";
}
cout << endl;
}
return ans;
}
int main(void) {
vector<int> _window = {2, 3, 4, 2, 6, 2, 5, 1};
int _max = getMaxValInWindow(_window, 3);
cout << "the max value of slide window is " << _max << endl;
return 0;
}