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/ ...
随机推荐
- nginx 几个常用的标准模块介绍
ngx_http_ssl_module(https) 1:指明是否启用的虚拟主机的ssl功能 ssl on | off; 2:指明虚拟主机使用的证书文件 ssl_certificate /usr/lo ...
- 201621123037 《Java程序设计》第8周学习总结
作业08-集合 1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 答: 思维导图: 其他-笔记: 2. 书面作业 1. ArrayList代码分析 1.1 解释ArrayLi ...
- PAT 甲级 1008 Elevator
https://pintia.cn/problem-sets/994805342720868352/problems/994805511923286016 The highest building i ...
- LR监控apache服务器
开启mod_status模块功能,在LR的controller中找到apache资源图双击并右键添加度量,如下图: 添加apache服务器IP地址.选择系统平台.添加需要监控的计数器即可进行 ...
- React Native 学习-组件说明和生命周期
组件的详细说明(Component Specifications) 当通过调用 React.createClass() 来创建组件的时候,你应该提供一个包含 render 方法的对象,并且也可以包含其 ...
- mysql 中文字段排序
方法1)select * from mytable order by CONVERT(chineseColumnName USING gbk); (备注:chineseColumnName 位排序字 ...
- ZOJ3113_John
这个题目是一个典型的Anti_Sg.我也不知道为什么这么叫,呵呵,反正大家都这么叫,而且我也是听别人说,看别人的日志自己才知道的. 题目的意思是给你不同颜色的石子,每次可以去一种颜色的石子若干个(至少 ...
- 【bzoj4321】queue2 dp
题目描述 n 个沙茶,被编号 1~n.排完队之后,每个沙茶希望,自己的相邻的两人只要无一个人的编号和自己的编号相差为 1(+1 或-1)就行: 现在想知道,存在多少方案满足沙茶们如此不苛刻的条件. ...
- C++解析(24):抽象类和接口、多重继承
0.目录 1.抽象类和接口 1.1 抽象类 1.2 纯虚函数 1.3 接口 2.被遗弃的多重继承 2.1 C++中的多重继承 2.2 多重继承的问题一 2.3 多重继承的问题二 2.4 多重继承的问题 ...
- 【转】Oracle 查询库中所有表名、字段名、表名说明、字段名说明
转自 :http://gis-conquer.blog.sohu.com/170243422.html 查询所有表名:select t.table_name from user_tables t; 查 ...