-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnumber-of-airplanes-in-the-sky.cpp
149 lines (133 loc) · 3.68 KB
/
number-of-airplanes-in-the-sky.cpp
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Time: O(nlogn)
// Space: O(n)
/**
* Definition of Interval:
* classs Interval {
* int start, end;
* Interval(int start, int end) {
* this->start = start;
* this->end = end;
* }
*/
class Solution {
public:
/**
* @param intervals: An interval array
* @return: Count of airplanes are in the sky.
*/
int countOfAirplanes(vector<Interval> &airplanes) {
int max_planes = 0;
int planes = 0;
vector<int> starts, ends;
for (const auto& i : airplanes) {
starts.emplace_back(i.start);
ends.emplace_back(i.end);
}
sort(starts.begin(), starts.end());
sort(ends.begin(), ends.end());
int s = 0, e = 0;
while (s < starts.size()) {
if (starts[s] < ends[e]) {
++planes;
max_planes = max(max_planes, planes);
++s;
} else {
--planes;
++e;
}
}
return max_planes;
}
};
class Solution2 {
public:
using Endpoint = enum {START, END};
struct Point {
Endpoint type;
int time;
};
struct Compare {
bool operator() (const Point&a, const Point&b) {
return a.time != b.time ? a.time < b.time : a.type > b.type;
}
};
/**
* @param intervals: An interval array
* @return: Count of airplanes are in the sky.
*/
int countOfAirplanes(vector<Interval> &airplanes) {
int max_planes = 0;
int planes = 0;
vector<Point> points;
for (const auto& i : airplanes) {
points.emplace_back(Point{START, i.start});
points.emplace_back(Point{END, i.end});
}
sort(points.begin(), points.end(), Compare());
for (const auto& i : points) {
if (i.type == START) {
++planes;
max_planes = max(max_planes, planes);
} else {
--planes;
}
}
return max_planes;
}
};
// BST solution.
class Solution3 {
public:
struct Compare {
bool operator() (const Interval&a, const Interval&b) {
return a.start != b.start ? a.start < b.start : a.end < b.end;
}
};
/**
* @param intervals: An interval array
* @return: Count of airplanes are in the sky.
*/
int countOfAirplanes(vector<Interval> &airplanes) {
int max_planes = 0;
multiset<int> bst;
sort(airplanes.begin(), airplanes.end(), Compare());
for (const auto& i : airplanes) {
bst.emplace(i.end);
while (*bst.begin() <= i.start) {
bst.erase((bst.begin()));
}
if (bst.size() > max_planes) {
max_planes = bst.size();
}
}
return max_planes;
}
};
// Heap solution.
class Solution4 {
public:
struct Compare {
bool operator() (const Interval&a, const Interval&b) {
return a.start != b.start ? a.start < b.start : a.end < b.end;
}
};
/**
* @param intervals: An interval array
* @return: Count of airplanes are in the sky.
*/
int countOfAirplanes(vector<Interval> &airplanes) {
int max_planes = 0;
priority_queue<int, vector<int>, greater<int>> min_heap;
sort(airplanes.begin(), airplanes.end(), Compare());
for (const auto& i : airplanes) {
min_heap.emplace(i.end);
while (min_heap.top() <= i.start) {
min_heap.pop();
}
if (min_heap.size() > max_planes) {
max_planes = min_heap.size();
}
}
return max_planes;
}
};