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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. [LintCode] Number of Islands(岛屿个数)

    描述 给一个01矩阵,求不同的岛屿的个数. 0代表海,1代表岛,如果两个1相邻,那么这两个1属于同一个岛.我们只考虑上下左右为相邻. 样例 在矩阵: [ [1, 1, 0, 0, 0], [0, 1, ...

  4. [LintCode] Number of Islands 岛屿的数量

    Given a boolean 2D matrix, find the number of islands. Notice 0 is represented as the sea, 1 is repr ...

  5. LintCode "Number of Islands II"

    A typical Union-Find one. I'm using a kinda Union-Find solution here. Some boiler-plate code - yeah ...

  6. LintCode: Number of Islands

    分析:经典连通分量问题 图: 节点:所有1的位置 边:两个相邻的1的位置有一条边 BFS/DFS (DFS使用递归,代码较短) 选一个没标记的点,然后搜索,扩展4个邻居(如果有),直到不能扩展 每一次 ...

  7. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  8. Sweep Line

    391. Number of Airplanes in the Sky https://www.lintcode.com/problem/number-of-airplanes-in-the-sky/ ...

  9. [MeetCoder] Count Pairs

    Count Pairs Description You are given n circles centered on Y-aixs. The ith circle’s center is at po ...

随机推荐

  1. jQuery把所有被选中的checkbox的某个属性值连接成字符串

    有这样的一个需求:对于一组checkbox,当点击每个checkbox后,把当前处于选中状态的checkbox的某个属性值取出来连接成字符串,并以逗号分开. html部分: <input typ ...

  2. Android 数据存储02之文件读写

    Android文件读写 版本 修改内容 日期 修改人 V1.0 原始版本 2013/2/25 skywang Android文件读写的有两种方式.一种,是通过标准的JavaIO库去读写.另一种,是通过 ...

  3. 一键GHOST优盘版安装XP/win7系统

    系统的安装方法有各种各样,一键GHOST优盘版也是其中的一种系统安装方法,也是俗称的U盘系统安装.下面豆豆来详细介绍下使用一键GHOST优盘版系统安装方法. 一.安装: 所谓"优盘" ...

  4. Java 下载文件

    public @ResponseBody void exportExcel(HttpServletRequest request, HttpServletResponse response, Khxx ...

  5. oracle监听1067错误的处理

    一,oracle监听1067错误的处理修改oracle安装目录D:\DataBase\oracle\product\10.1.0\Db_1\NETWORK\ADMIN\下的 listener.ora和 ...

  6. Matlab Codes and Datasets for Feature Learning

    Matlab Codes and Datasets for Feature Learning 浙江大学CAiDeng提供的Matlab特征学习Code.

  7. 破产姐妹第六季/全集2 Broke Girls迅雷下载

    英文全名2 Broke Girls,第6季(2016)CBS. 本季看点:<破产姐妹>第五季季终集达到了收视人数699万收视率1.7.在第五季终集,两姐妹发现Han(马修·摩伊 饰)的生活 ...

  8. Visual Studio 2013 智能提示功能消失解决办法

    Visual Studio 2013中,智能提示功能突然用不了,查了一下,使用命令行重置VS的方法解决了这个问题.步骤如下: 开始菜单 -->所有程序-->Visual Studio 20 ...

  9. 模型标准化——预测模型标记语言(PMML)

    https://www.cnblogs.com/pinard/p/9220199.html 在机器学习用于产品的时候,我们经常会遇到跨平台的问题.比如我们用Python基于一系列的机器学习库训练了一个 ...

  10. 【转】group_concat函数详解

    转自: http://hchmsguo.iteye.com/blog/555543 问了好多人,都不知道group_concat这个函数. 这个函数好啊,能将相同的行组合起来,省老事了. MySQL中 ...