LintCode: Number of Airplanes in the Sky
C++
(1)把interval数组中的所有start和所有end放在同一个数组中,然后进行排序,遇到start就起飞一架飞机,遇到一架end就降落一架飞机,所以start有个+1属性,end有个-1属性,这样就可以根据排序之后的数组得知任意时间飞行中的飞机的数量了
(2)pair,make_pair,val.first,val.second
(3)sort(), max()
(4)for (auto &i : Object) {}
(5)push_back()
/**
* 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
vector<pair<int, int> > v;
for (auto &i : airplanes) {
v.push_back(make_pair(i.start, ));
v.push_back(make_pair(i.end, -));
}
int cnt = , res = ;
sort(v.begin(), v.end());
for (auto &i : v) {
cnt += i.second;
res = max(cnt, res);
}
return res;
}
};
另一个解法(无需排序):
刚开始阅读题目的时候,我以为起飞时间和降落时间都是0~24之内,所以就用了两个长度30的辅助数组,这种方法可以避免排序。
但是,无法AC,因为interval中的元素,最大的有几十万,如果辅助数组也这么大的话,那很容易超时。
/**
* 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
int fly[] = {};
int land[] = {};
int len = airplanes.size();
for (int i = ; i < len; i++ ) {
fly[airplanes[i].start] ++;
land[airplanes[i].end] --;
}
int max = , cur = ;
for (int i = ; i < ; i++ ) {
cur = cur + fly[i] + land[i];
if (cur > max) {
max = cur;
}
}
return max;
}
};
LintCode: Number of Airplanes in the Sky的更多相关文章
- Lintcode391 Number of Airplanes in the Sky solution 题解
[题目描述] Given an interval list which are flying and landing time of the flight. How many airplanes ar ...
- 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 ...
- [LintCode] Number of Islands(岛屿个数)
描述 给一个01矩阵,求不同的岛屿的个数. 0代表海,1代表岛,如果两个1相邻,那么这两个1属于同一个岛.我们只考虑上下左右为相邻. 样例 在矩阵: [ [1, 1, 0, 0, 0], [0, 1, ...
- [LintCode] Number of Islands 岛屿的数量
Given a boolean 2D matrix, find the number of islands. Notice 0 is represented as the sea, 1 is repr ...
- LintCode "Number of Islands II"
A typical Union-Find one. I'm using a kinda Union-Find solution here. Some boiler-plate code - yeah ...
- LintCode: Number of Islands
分析:经典连通分量问题 图: 节点:所有1的位置 边:两个相邻的1的位置有一条边 BFS/DFS (DFS使用递归,代码较短) 选一个没标记的点,然后搜索,扩展4个邻居(如果有),直到不能扩展 每一次 ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- Sweep Line
391. Number of Airplanes in the Sky https://www.lintcode.com/problem/number-of-airplanes-in-the-sky/ ...
- [MeetCoder] Count Pairs
Count Pairs Description You are given n circles centered on Y-aixs. The ith circle’s center is at po ...
随机推荐
- AngularJS使用OData请求ASP.NET Web API资源的思路
本篇整理AngularJS使用OData请求ASP.NET Web API资源的思路. 首先给ASP.NET Web API插上OData的翅膀,通过NuGet安装OData. 然后,给control ...
- 在ASP.NET MVC下限制同一个IP地址单位时间间隔内的请求次数
有时候,当用户请求一个Controller下的Action,我们希望,在单位时间间隔内,比如每秒,每分钟,每小时,每天,每星期,限制同一个IP地址对某个Action的请求次数.如何做呢? stefan ...
- 利用MPMoviePlayerViewController 播放视频 iOS
方法一: @property (nonatomic, strong) MPMoviePlayerController *player; NSString *url = [[NSBundle mainB ...
- 学习 HTML+CSS 的书籍推荐
1.<CSS那些事儿> 本书专注于CSS技巧实例的讲解,由浅入深地分析了CSS样式在布局时所需要理解的原理.绕开随处可见的基础知识.网络中能随意搜索到的hack技巧,侧重原理分析,拓展读者 ...
- fastjson 过滤不需要的字段或者只要某些字段
/* * 第一种:在对象响应字段前加注解,这样生成的json也不包含该字段. * @JSONField(serialize=false) * private String name; */ / ...
- springMVC中HTTP PUT请求该如何传输请求参数呢?
对于表单提交,tomcat默认只解析POST的表单,对于PUT和DELETE的不处理,所以Spring拿不到.解决方案:1.修改tomcat的server.xml: <Connector p ...
- Oracle初级索引学习总结
前言 索引是常见的数据库对象,建立索引的目的是为了提高记录的检索速度.它的设置好坏,使用是否得当,极大地影响数据库应用程序和Database的性能.虽然有许多资料讲索引的用法,DBA和Develop ...
- Android之文件搜索工具类
/** * @detail 搜索sdcard文件 * @param 需要进行文件搜索的目录 * @param 过滤搜索文件类型 * */ private void search(File file, ...
- 最课程启示录:L风的李同学
李同学是一个特殊的存在. 他永远是学员群里话最多的一个男同学.注意,这里加了一个定语“男”,这当然意味着不久的将来,我们的学员启示录将会出现一个话多的女同学. 我并不是第一天知道他话多.据说,他在来最 ...
- 《成神之路-基础篇》JVM——Java内存模型(已完结)
Java内存模型 本文是<成神之路系列文章>的第一篇,主要是关于JVM的一些介绍. 持续更新中 Java内存模型 JVM内存结构 VS Java内存模型 VS Java对象模型(Holli ...