Given an interval list which are flying and landing time of the flight. How many airplanes are on the sky at most?

Notice

If landing and flying happens at the same time, we consider landing should happen at first.

Example

For interval list

[
[1,10],
[2,3],
[5,8],
[4,7]
]

Return 3

分析:

这题可以从1-23逐个check是否interval含有那个数。但是这题有一个更巧妙的方法:

把所有的interval分成两个部分,起飞部分和落地部分。把每个部分都加在LIST里面,然后按照时间排序,然后一一取出。如果是起飞,count++, 如果落地,count--。这个解法是不是很奇特。

如果我是面试官,我可能会换一种方式问:给一堆intervals,找出overlap最多的个数,这样,第一种解法就直接不能最为最优解了。

 /**
* Definition of Interval:
* public classs Interval {
* int start, end;
* Interval(int start, int end) {
* this.start = start;
* this.end = end;
* }
*/ public class Solution {
/**
* @param intervals: An interval array
* @return: Count of airplanes are in the sky.
*/
public int countOfAirplanes(List<Interval> airplanes) { List<Point> list = new ArrayList<Point>(airplanes.size()*);
for(Interval i : airplanes){
list.add(new Point(i.start, ));
list.add(new Point(i.end, ));
} Collections.sort(list);
int count = , ans = ;
for(Point p : list){
if(p.flag == ) count++;
else count--;
ans = Math.max(ans, count);
} return ans;
}
} class Point implements Comparable<Point> {
int time;
int flag; Point(int t, int s) {
this.time = t;
this.flag = s;
} @Override
public int compareTo(Point p) {
if (this.time == p.time)
return this.flag - p.flag;
else
return this.time - p.time;
}
}

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. LintCode: Number of Airplanes in the Sky

    C++ (1)把interval数组中的所有start和所有end放在同一个数组中,然后进行排序,遇到start就起飞一架飞机,遇到一架end就降落一架飞机,所以start有个+1属性,end有个-1 ...

  3. [LintCode]——目录

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

  4. [MeetCoder] Count Pairs

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

  5. Sweep Line

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

  6. Sky number

    描述 key 天生对数字特别敏感,一次偶然的机会,他发现了一个有趣的四位数2992,这个数,它的十进制数表示,其四位数字之和为2+9+9+2=22,它的十六进 制数BB0,其四位数字之和也为22,同时 ...

  7. Sky数[HDU2097]

    Sky数 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  8. hdoj 2097 Sky数

    Sky数 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  9. HDOJ(HDU) 2097 Sky数(进制)

    Problem Description Sky从小喜欢奇特的东西,而且天生对数字特别敏感,一次偶然的机会,他发现了一个有趣的四位数2992,这个数,它的十进制数表示,其四位数字之和为2+9+9+2=2 ...

随机推荐

  1. [JSP] c:forEach 如何输出序号

    关键在于<c:forEach>的varStatus属性,具体代码如下: <table width="500" border="0" cells ...

  2. Fibbing以让虚结点的设置更简单为目的优化网络需求

  3. Feature List

    我组最终决定所做的软件工程项目是Bing词典(UWP)的背单词模块,下面是初步定下的Feature List. 按用户场景变化顺序列举(假设是新用户): 1.用户可通过点击“背单词”标识或按钮进入背单 ...

  4. Cooperate with Myself

    (一) 第一周的第一批作业们.  且不说一周之内要看完我们的300多页的教材,也不说需要在维基的大批量的文献中海底捞针,单是这个四则运算的生成程序就让我从假期的迷糊状态中幡然觉悟了:哦!惊险刺激的新的 ...

  5. input 清空值。(转载)

    ref顾名思义我们知道,其实它就可以被看座是一个组件的参考,也可以说是一个标识.作为组件的属性,其属性值可以是一个字符串也可以是一个函数. 其实,ref的使用不是必须的.即使是在其适用的场景中也不是非 ...

  6. vue 请求后台数据2(copy)

    https://blog.csdn.net/vergilgeekopen/article/details/68954940 需要引用vue-resource 安装请参考https://github.c ...

  7. react & monaco editor & vs code

    react & monaco editor & vs code monaco-editor https://microsoft.github.io/monaco-editor/inde ...

  8. 一本通1645Fibonacci

    1645:Fibonacci 时间限制: 1000 ms         内存限制: 524288 KB [题目描述] 原题来自:POJ 3070 我们知道斐波那契数列 F0=0,F1=1,Fn=Fn ...

  9. BZOJ2460 Beijing2011元素(线性基+贪心)

    按价值从大到小考虑每个元素,维护一个线性基,如果向其中加入该元素的编号仍然构成线性基,则将其加入. 不会证明.当做线性基的一个性质吧. #include<iostream> #includ ...

  10. 【刷题】LOJ 2818 「eJOI2018」循环排序

    题目描述 本题译自 eJOI2018 Problem F「Cycle Sort」 给定一个长为 \(n\) 的数列 \(\{a_i\}\) ,你可以多次进行如下操作: 选定 \(k\) 个不同的下标 ...