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. 玩一下C#的语音识别

    在.NET4.0中,我可以借助System.Speech组件让电脑来识别我们的声音. 以上,当我说"name",显示"Darren",我说"age&q ...

  2. javascript:Array.prototype.slice.call(arguments)

    我们知道,Array.prototype.slice.call(arguments)能将具有length属性的对象转成数组,除了IE下的节点集合(因为ie下的dom对象是以com对象的形式实现的,js ...

  3. 组织安全性SQL

    BEGIN mo_global.set_policy_context ('S', '&组织ID'); END; SELECT * FROM hr_organization_units_v;   ...

  4. LinkedList、Stack、Queue、PriorityQueue的总结

    1.这几种容器的特点 都是按照顺序来存储元素. 取元素的时候的不同点: LinkedList:按照下标随意取元素 Stack:后进先出取元素 Queue:先进先出取元素 PriorityQueue:按 ...

  5. 识骨寻踪第十二季/全集Bones迅雷下载

    本季 Bones (2015)看点:<识骨寻踪>(FOX)2005年推出的罪案题材的电视连续剧.该剧部分内容改编自前刑侦检验官.现任该剧制作人凯丝·莱克斯出版的一系列侦探小说.Bones的 ...

  6. ARCH模型

    ARCH模型的基本思想 ARCH模型的基本思想是指在以前信息集下,某一时刻一个噪声的发生是服从正态分布.该正态分布的均值为零,方差是一个随时间变化的量(即为条件异方差).并且这个随时间变化的方差是过去 ...

  7. 一致性哈希算法(consistent hashing)(转)

    原文链接:每天进步一点点——五分钟理解一致性哈希算法(consistent hashing)  一致性哈希算法在1997年由麻省理工学院提出的一种分布式哈希(DHT)实现算法,设计目标是为了解决因特网 ...

  8. Win7局域网打印机共享设置(详细图文流程)

    本文将向读者说明在Win7下如何实现同个局域网内共享打印机.经过测试,Win7之间和Win7与XP之间均可正常连接. 第一步:取消禁用Guest用户 1. 点击[开始]按钮,在[计算机]上右键,选择[ ...

  9. 自定义View 可清除内容、设置图标、下划线的输入框 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  10. extern外部方法使用C#简单例子

    外部方法使用C#简单例子 1.增加引用using System.Runtime.InteropServices; 2.声明和实现的连接[DllImport("kernel32", ...