[LintCode] Number of Airplanes in the Sky

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 };
时间: 2024-10-23 13:50:22

[LintCode] Number of Airplanes in the Sky的相关文章

391. Number of Airplanes in the Sky

391. Number of Airplanes in the Sky 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 Notice If landing and flying hap

Number of Airplanes in the Sky

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. Example For interval list [ [1,10],

lintcode-medium-Number of Airplanes in the Sky

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. Example For interval list [ [1,10],

[LintCode] Number of Islands 岛屿的数量

Given a boolean 2D matrix, find the number of islands. Notice 0 is represented as the sea, 1 is represented as the island. If two 1 is adjacent, we consider them in the same island. We only consider up/down/left/right adjacent. Have you met this ques

LintCode &quot;Number of Islands II&quot;

A typical Union-Find one. I'm using a kinda Union-Find solution here. Some boiler-plate code - yeah I know. class Solution { unordered_set<int> hs; // start from 1 int max_k; public: void dfs(vector<vector<int>> &mt, int x, int y, in

Sweep Line

391. Number of Airplanes in the Sky https://www.lintcode.com/problem/number-of-airplanes-in-the-sky/description?_from=ladder&&fromId=4 思路:将起飞时间和降落时间放到同一个数组中, 标识出是起飞还是降落时间, 然后对数组排序,遍历数组即可, 碰到起飞计数器加一, 碰到降落计数器减一. 维护最大值作为答案. 注意降落优先于起飞 可通过flag标记降落为0 上升

[MeetCoder] Count Pairs

Count Pairs Description You are given n circles centered on Y-aixs. The ith circle’s center is at point (i, 0) and its radius is A[i]. Count the number of pairs of circles that have at least one common point? Input The input should be a list of n pos

Sky number

描述 key 天生对数字特别敏感,一次偶然的机会,他发现了一个有趣的四位数2992,这个数,它的十进制数表示,其四位数字之和为2+9+9+2=22,它的十六进 制数BB0,其四位数字之和也为22,同时它的十二进制数表示1894,其四位数字之和也为22.key非常喜欢这种四位数(三种进制的和相等),由于他 的发现,所以这里我们命名其为key数.但是要判断这样的数还是有点麻烦啊,那么现在请你帮忙来判断任何一个十进制的四位数,是不是key数吧. 输入 输入含有一些四位正整数,如果为0,则输入结束. 输

NYOJ-Sky number

Sky number 时间限制:1000 ms  |  内存限制:65535 KB 难度:1 描述 key天生对数字特别敏感,一次偶然的机会,他发现了一个有趣的四位数2992,这个数,它的十进制数表示,其四位数字之和为2+9+9+2=22,它的十六进制数BB0,其四位数字之和也为22,同时它的十二进制数表示1894,其四位数字之和也为22.key非常喜欢这种四位数(三种进制的和相等),由于他的发现,所以这里我们命名其为key数.但是要判断这样的数还是有点麻烦啊,那么现在请你帮忙来判断任何一个十进