LintCode 391: Count Of Airplanes
LintCode 391: Count Of Airplanes
题目描述
给出飞机的起飞和降落时间的列表,用 interval 序列表示. 请计算出天上同时最多有多少架飞机?
样例
对于每架飞机的起降时间列表:[[1,10],[2,3],[5,8],[4,7]], 返回3。
Thu Feb 23 2017
思路
这道题思路很容易想到,即将飞机起飞降落的过程模拟一遍,即可得到最大飞机数。
首先将Interval序列按照起飞时间排序,以及再按照降落时间排序,然后从最早起飞时间到最晚降落时间遍历,降落一架飞机,计数器减一,起飞一家飞机,计数器加一。
时间复杂度是\(O(n+m)\),从复杂度上是最优的了,但是本题还有更好的方法,以后再补吧。
代码
// 数飞机
// 用于排序起飞时间的比较函数
static bool cmp_start(const Interval& x, const Interval& y)
{
return x.start < y.start;
}
// 用于排序降落时间的比较函数
static bool cmp_end(const Interval& x, const Interval& y)
{
return x.end < y.end;
}
// 主执行函数
int countOfAirplanes(vector<Interval> &airplanes)
{
if (airplanes.size() <= 0) return 0;
vector<Interval> airplanes_start(airplanes);
vector<Interval> airplanes_end(airplanes);
sort(airplanes_start.begin(), airplanes_start.end(), cmp_start);
sort(airplanes_end.begin(), airplanes_end.end(), cmp_end);
int max_count = -1, now_count = 0;
int start_index = 0, end_index = 0;
for (int i = airplanes_start.begin()->start; i < (airplanes_end.end() - 1)->end; ++i)
{
if (airplanes_end[end_index].end == i)
{
--now_count;
++end_index;
}
else if (airplanes_start[start_index].start == i)
{
++now_count;
++start_index;
}
else continue;
if (now_count > max_count) max_count = now_count;
// 由于在一个时间点可能同时有降落和起飞,所以需要回退一次
--i;
}
return max_count;
}
LintCode 391: Count Of Airplanes的更多相关文章
- lintcode :Count and Say 报数
题目: 报数 报数指的是,按照其中的整数的顺序进行报数,然后得到下一个数.如下所示: 1, 11, 21, 1211, 111221, ... 1 读作 "one 1" -> ...
- lintcode :Count 1 in Binary 二进制中有多少个1
题目: 二进制中有多少个1 49% 通过 计算在一个 32 位的整数的二进制表式中有多少个 1. 样例 给定 32 (100000),返回 1 给定 5 (101),返回 2 给定 1023 (111 ...
- LintCode "Triangle Count"
Should be "Medium" or even "Easy".. Just with a little Greedy. class Solution { ...
- LintCode: Number of Airplanes in the Sky
C++ (1)把interval数组中的所有start和所有end放在同一个数组中,然后进行排序,遇到start就起飞一架飞机,遇到一架end就降落一架飞机,所以start有个+1属性,end有个-1 ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- lintcode算法周竞赛
------------------------------------------------------------第七周:Follow up question 1,寻找峰值 寻找峰值 描述 笔记 ...
- lintcode:数飞机
数飞机 给出飞机的起飞和降落时间的列表,用 interval 序列表示. 请计算出天上同时最多有多少架飞机? 如果多架飞机降落和起飞在同一时刻,我们认为降落有优先权. 样例 对于每架飞机的起降时间列表 ...
- Number of Airplanes in the Sky
Given an interval list which are flying and landing time of the flight. How many airplanes are on th ...
- Sweep Line
391. Number of Airplanes in the Sky https://www.lintcode.com/problem/number-of-airplanes-in-the-sky/ ...
随机推荐
- 3dContactPointAnnotationTool开发日志(二六)
之前给老师看了看我的毕设,老师觉得操作太复杂了,要能像3ds max里那样可以拖动物体的轴进行平移,沿着显示的圆圈旋转以及缩放啥的.说白了就是在Unity3d的Game视图显示出Scene视图里的 ...
- 2."结对项目"的心得体会
上个星期,老师给我们布置了个课堂小作业: 某公司程序员二柱的小孩上了小学二年级,老师让家长每天出30道(100以内)四则运算题目给小学生做.二柱立马就想到写一个小程序来做这件事. 这个事情可以用很 ...
- SSH框架配置
--------------------------------applicationContext.xml-------------------------------- <?xml vers ...
- eclipse官方网址、各个版本的下载
Eclipse3.1后各版本代号 (2013-07-10 20:48:42) 转载▼ 分类: Java Eclipse 3.1 版本代号 IO [木卫1,伊奥] Eclipse 3.2 版本代号 ...
- 第144天:PS切图方法总结
一.切图方法分类 PhotoShop从CS版本演变到现在的CC版本,切图功能发生了比较大的变化,我们可以把PhotoShop CS版本时的切图功能称为传统切图,而从PhotoShop CC版本开始PS ...
- get 与 next()
- 转--- 秒杀多线程第七篇 经典线程同步 互斥量Mutex
阅读本篇之前推荐阅读以下姊妹篇: <秒杀多线程第四篇一个经典的多线程同步问题> <秒杀多线程第五篇经典线程同步关键段CS> <秒杀多线程第六篇经典线程同步事件Event& ...
- BZOJ5073 小A的咒语(动态规划)
设f[i][j][0/1]为前i位选j段时其中第i位选/不选最多能匹配到哪,转移时f[i][j][0]→f[i+1][j][0],f[i][j][1]→f[i+1][j][0],f[i][j][1]→ ...
- P2323 [HNOI2006]公路修建问题
题目描述 输入输出格式 输入格式: 在实际评测时,将只会有m-1行公路 输出格式: 输入输出样例 输入样例#1: 4 2 5 1 2 6 5 1 3 3 1 2 3 9 4 2 4 6 1 输出样例# ...
- 【转】Unable to load embedded resource from assembly 无法加载的程序集嵌入的资源
转自:http://blog.sina.com.cn/s/blog_994678b90101f035.html 项目运用IbatisNet 今天更新项目,编译完点击运行,报错如下: [“/”应用程序中 ...