您的位置:首页 > 大数据 > 人工智能

lintcdoe: Number of Airplanes in the Sky

2016-02-27 23:24 465 查看

Number of Airplanes in the Sky

30:00

Given an interval list which are flying and landing time of the flight. How many airplanes are on the sky at most?

Have you met this question in a real interview?

Notice

If landing and flying happens at the same time, we consider landing should happen at first.

Example
Tags
Related Problems
Notes

For interval list
[[1,10],[2,3],[5,8],[4,7]]
, return
3


/**
* 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; //因为首先要有一只飞机在天上飞,所以i要先于j

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;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: