Given an interval list which are flying and landing time of the flight. How many airplanes are on the sky at most?
Example
For interval list [[1,10],[2,3],[5,8],[4,7]]
, return 3
Note
If landing and flying happens at the same time, we consider landing should happen at first.
Tags Expand
/**
* 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) {
// write your code here
vector<int> departure;
vector<int> landing;
for (int i=0; i< airplanes.size(); i++)
{
departure.push_back(airplanes[i].start);
landing.push_back(airplanes[i].end);
}
sort(departure.begin(), departure.end());
sort(landing.begin(), landing.end());
int airplane_in_sky = 1;
int most_airplane_in_sky = 1;
int i=1; int j=0;
while (i < departure.size() && j < landing.size())
{
if (departure[i] < landing[j])
{
airplane_in_sky++;
if (airplane_in_sky > most_airplane_in_sky)
{
most_airplane_in_sky = airplane_in_sky;
}
i++;
}
else
{
airplane_in_sky--;
j++;
}
}
return most_airplane_in_sky;
}
};