LintCode-数飞机
来源:互联网 发布:收淘宝天猫评价的软件 编辑:程序博客网 时间:2024/04/27 15:30
给出飞机的起飞和降落时间的列表,用 interval 序列表示. 请计算出天上同时最多有多少架飞机?
样例
对于每架飞机的起降时间列表:[[1,10],[2,3],[5,8],[4,7]]
, 返回3
。
注意
如果多架飞机降落和起飞在同一时刻,我们认为降落有优先权。
分析:可以根据每个线段,利用一个map来标记,对于第一个[1,10],我们标记m[1]+=1,m[10]-=1,表示在1这个时刻多了一架飞机,而在10这个时候少了一架飞机,即用m记录飞机的变化数目。注意:如果降落不具有优先权的话,也就是说在10这个时候我们可以认为还在天上的话,那么应该设m[11]-=1
代码:
/** * 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 map<int,int> m; set<int> s; for(int i=0;i<airplanes.size();i++) { Interval in = airplanes[i]; m[in.start]++; m[in.end]--; s.insert(in.start); s.insert(in.end); } vector<int> t; copy(s.begin(),s.end(),back_inserter(t));// already sorted int ret = 0; int cnt = 0; for(auto x:t) { cnt+=m[x]; ret = max(ret,cnt); } return ret; }};
0 0
- LintCode-数飞机
- LintCode--数飞机
- lintcode-数飞机-391
- LintCode :数飞机
- Lintcode:数飞机
- Lintcode:数飞机
- lintcode(391)数飞机
- lintcode-数飞机
- 数飞机 -LintCode
- lintcode--数飞机
- LintCode 数飞机的两种解法
- LintCode --number-of-airplanes-in-the-sky(数飞机)
- 数飞机
- Lintcode 两数之和
- LintCode 三数之和
- LintCode-最大数
- LintCode-交错正负数
- LintCode 丑数
- BZOJ2435【TreeDP】
- KVC/KVO原理详解及编程指南
- 零基础学通C语言,福利来啦!!!!zfhl.ke.qq.com
- Ada语言与传统C语言的异同
- [leetcode][two pointers] Trapping Rain Water
- LintCode-数飞机
- 过拟合(overfitting) vs 正则化(regularization)
- 利用指针控制数组大小过程
- POJ 2479
- [Windows通用应用开发] 读写XML文件
- 11572 - Unique Snowflakes
- GCD使用经验与技巧浅谈
- 学习方法总结
- python中利用Cookielib获取网页的登录的cookie