lintcdoe: Number of Airplanes in the Sky

来源:互联网 发布:linux系统查看ip地址 编辑:程序博客网 时间:2024/06/10 09:47

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?

Notice

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

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;    }};


0 0
原创粉丝点击