-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcountOfAirplanes.cpp
More file actions
91 lines (86 loc) · 2.47 KB
/
countOfAirplanes.cpp
File metadata and controls
91 lines (86 loc) · 2.47 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* Definition of Interval:
* classs Interval {
* int start, end;
* Interval(int start, int end) {
* this->start = start;
* this->end = end;
* }
*/
class myPoint {
public:
int startTime;
int isStart;
myPoint(int _startTime, bool _isStart){
this->startTime = _startTime;
this->isStart = _isStart;
}
// writing sort compare funtion is a pain!!!!
// const ... const is must! or else compile error
#if 0
bool operator ()(const myPoint &a, const myPoint &b) const{
if(a.startTime == b.startTime){
return a.isStart < b.isStart;
}
else{
return a.startTime < b.startTime;
}
}
#endif
// const ... const is must! or else compile error
bool operator <(const myPoint &obj) const{
if(startTime == obj.startTime){
return isStart < obj.isStart;
}
else{
return startTime < obj.startTime; // 从小到大排序
}
}
bool operator >(const myPoint &obj) const{
if(startTime == obj.startTime){
return isStart > obj.isStart;
}
else{
return startTime > obj.startTime;
}
}
};
// 注意此处定义myPoint 与 sort对象vector<myPoint>参数类型要一致
bool mycompare(const myPoint &a, const myPoint &b){
if(a.startTime == b.startTime){
return a.isStart < b.isStart;
}
else{
return a.startTime < b.startTime;
}
}
class Solution {
public:
/**
* @param intervals: An interval array
* @return: Count of airplanes are in the sky.
*/
int countOfAirplanes(vector<Interval> &airplanes) {
// write your code here
// 注意此处定义myPoint 与 mycompare 函数参数类型要一致
vector<myPoint> points;
for(Interval itv : airplanes){
points.push_back(myPoint(itv.start, 1));
points.push_back(myPoint(itv.end, 0));
}
sort(points.begin(), points.end());
//sort(points.begin(), points.end(), mycompare);
int count = 0;
int maxCount = 0;
for(int i = 0; i < points.size(); i++){
if(points[i].isStart){
count++;
}
else{
count--;
}
maxCount = max(maxCount, count);
}
return maxCount;
}
};