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 ...
随机推荐
- 内存映射函数remap_pfn_range学习——代码分析(3)
li {list-style-type:decimal;}ol.wiz-list-level2 > li {list-style-type:lower-latin;}ol.wiz-list-le ...
- delphi中单独编译pas生成dcu文件
delphi中单独编译pas生成dcu文件 在网上下载了一个带源码的组件,结果碰到提示说缺少xxx.dcu.一看它的目录下确实没有,那能不能生成一个呢? 当然可以! 方法是使用delphi的安装目录\ ...
- The Topo to Raster tool returns errors 010235 and 010067转
Problem: The Topo to Raster tool returns errors 010235 and 010067 Description The Topo to Raster geo ...
- 浅析c++中virtual关键字
http://blog.csdn.net/djh512/article/details/8973606 1.virtual关键字主要是什么作用? c++中的函数调用默认不适用动态绑定.要触发动态绑定, ...
- 浴血黑帮第一季/全集Peaky Blinders迅雷下载
本季第一季Peaky Blinders Season 1 (2013)看点:<浴血黑帮>Peaky Blinders是从战后伯明翰地区走出的一个传奇黑帮家族,时间要追溯到1919年,家族成 ...
- Android之对TabActivity的见解,个人觉得不错
http://www.cnblogs.com/answer1991/archive/2012/05/08/2489844.html answer1991 无法停止我内心的狂热,对未来的执着. Andr ...
- 简析Window、Activity、DecorView以及ViewRoot之间的错综关系
一.职能简介 Activity Activity并不负责视图控制,它只是控制生命周期和处理事件.真正控制视图的是Window.一个Activity包含了一个Window,Window才是真正代表一个窗 ...
- Java并发编程的艺术(九)——批量获取多条线程的执行结果
当向线程池提交callable任务后,我们可能需要一次性获取所有返回结果,有三种处理方法. 方法一:自己维护返回结果 // 创建一个线程池 ExecutorService executorServic ...
- 通过layer-list自定义EditText背景
假设activity的背景是白色,第一层也(就是最底层)是绿色:第二次是白色,但是距离底部有一段小偏移,目的是为了做出文本框两边的小勾:第三层也是白色,但是它距离底部和左右两边都有一定距离.通过三层配 ...
- const 变量修饰 研究
#include<stdio.h> #include<iostream> using namespace std; struct A { ;} ;} }; int main() ...