-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.cpp
More file actions
50 lines (46 loc) · 1.09 KB
/
program.cpp
File metadata and controls
50 lines (46 loc) · 1.09 KB
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
47
48
49
50
#include "../include/prevector.h"
#include <queue>
#include <utility>
typedef std::pair<int, int> Elem;
struct Comp{
public:
bool operator()(Elem& a, Elem& b){
return (a.first < b.first);
}
};
//复杂度的渐进上界应该是 n log(n)
//Leetcode上耗时为132ms
//需要进一步研究
vector<int> maxSlidingWindow(vector<int>& nums, int k)
{
std::priority_queue<Elem, vector<Elem>, Comp> qe;
vector<int> rst;
int i = 0;
for ( ; i < k - 1; i++){
qe.push(std::make_pair(nums[i], i));
}
Elem e;
for ( ; i < nums.size(); i++){
qe.push(std::make_pair(nums[i], i));
e = qe.top();
while (e.second <= i - k){
qe.pop();
e = qe.top();
}
rst.push_back(e.first);
}
return rst;
}
int Mymain()
{
vector<int> nums = {1,3,-1,-3,5,3,6,7};
PrintVector(maxSlidingWindow(nums, 3));
//Boring!
//Can work on MSVC >= 1800,
//But not on Leetcode
//I will find out why, probably
auto comp = [](int i){return i + 1;};
decltype(comp) comp2;
cout << comp2(5);
return 0;
}