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. process.tar.gz

    exec1.c #include <stdio.h> #include <unistd.h> int main() { char *arglist[3]; arglist[0] ...

  2. 2017-2018 第一学期201623班《程序设计与数据结构》-第5&6周作业问题总结

    一.作业内容 第5周作业 http://www.cnblogs.com/rocedu/p/7484252.html#WEEK05 第6周作业 http://www.cnblogs.com/rocedu ...

  3. 2-Python3从入门到实战—基础之运算符

    Python从入门到实战系列--目录 Python语言支持以下类型的运算符: 算术运算符 比较(关系)运算符 赋值运算符 逻辑运算符 位运算符 成员运算符 身份运算符 运算符优先级 算术运算符 Pyt ...

  4. 【壹拾壹周】final_review

    项目名:俄罗斯方块 组名:新蜂 组长:武志远 组员:宫成荣 杨柳 谢孝淼 李桥 final Review会议 时间:2016.12.3 会议内容 设想和目标 1.在final阶段发布时的预期目标是什么 ...

  5. Hbase远程连接:Can't get the locations

    当Java API远程连接出错:Can't get the locations 原先填入的是IP地址,后来改为HOSTS文件中配置的主机名问题解决,如下红色字体部分: conf.set("h ...

  6. JSONP使用及注意事项小结

    什么是JSONP 三句话总结: 概念:JSONP(JSON with Padding)是JSON的一种"使用模式". 目的:用于解决主流浏览器的跨域数据访问的问题. 原理:利用 & ...

  7. BZOJ 3609: [Heoi2014]人人尽说江南好

    3609: [Heoi2014]人人尽说江南好 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 470  Solved: 336[Submit][Sta ...

  8. 在Mac OS X上用Fluid把网页变成本地App

    最近一直在个在线听音乐的解决方案,也下了很多的本地软件,什么酷狗.酷我.豆瓣.虾米.QQ.百度音乐之类的,下了一大堆,都逐个测试了,效果都不是很理想-- 要么是UI太悲催,要么是对Retina支持不友 ...

  9. 走楼梯(walk) 解题报告

    走楼梯(walk) 题意 给一个长为\(n(1\le n\le 10^5)\)序列\(\{a\}\),每次从中间挖掉\([l,r]\),然后询问最长上升子序列,强制在线. 有一档分是30000和离线, ...

  10. hdu5449 Robot Dog (树形dp+倍增lca)

    (警告:本篇博客包含大量人类本质内容) 先处理出来lca,然后就只需要知道从每个点到他的父亲.和从他的父亲到这个点的期望时间就可以了 我们设f[x]为x到他父亲的期望时间:g[x]为从x的父亲到x的期 ...