码迷,mamicode.com
首页 > 其他好文 > 详细

[LintCode] Number of Airplanes in the Sky

时间:2015-05-14 23:40:05      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:

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.

http://www.lintcode.com/en/problem/number-of-airplanes-in-the-sky/

将所有区间的start与end放在一起排序,但是要标记其是属性,然后统一排序,问题就转化成了括号匹配的问题了,只要用一个cnt来记录,遇到start就加1,遇到end就减1,记录过程中的最大值就是答案。 

 1 /**
 2  * Definition of Interval:
 3  * classs Interval {
 4  *     int start, end;
 5  *     Interval(int start, int end) {
 6  *         this->start = start;
 7  *         this->end = end;
 8  *     }
 9  */
10 class Solution {
11 public:
12     /**
13      * @param intervals: An interval array
14      * @return: Count of airplanes are in the sky.
15      */
16     int countOfAirplanes(vector<Interval> &airplanes) {
17         // write your code here
18         vector<pair<int, bool> > v;
19         for (auto &i : airplanes) {
20             v.push_back(make_pair(i.start, true));
21             v.push_back(make_pair(i.end, false));
22         }
23         int cnt = 0, res = 0;
24         sort(v.begin(), v.end());
25         for (auto &i : v) {
26             if (i.second) ++cnt;
27             else --cnt;
28             res = max(cnt, res);
29         }
30         return res;
31     }
32 };

 

[LintCode] Number of Airplanes in the Sky

标签:

原文地址:http://www.cnblogs.com/easonliu/p/4504647.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!