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;
}
}

最新文章

  1. React的井字过三关(1)
  2. 5-Zend Studio配置
  3. express框架路由配置及congtroller自动加载
  4. 利用ffmpeg给小视频结尾增加logo水印
  5. 删除共享内存后key为0x00000000的问题
  6. 使用RequireJS优化Web应用前端
  7. 使用MJRefresh遇到的坑
  8. 电子工程师名片——UFI Command,USB盘符的显示
  9. PHPSTORM下安装XDEBUG
  10. MAC ACL、RACL和VACL
  11. 浏览器中输入URL发生了什么
  12. idea 常见快捷键记录下
  13. SpringCloud-微服务配置统一管理SpringCloud Config(七)
  14. django 部署一个简单的博客系统
  15. My Sql数据库设置环境变量和字符集
  16. Sql Server 关于SET IDENTITY_INSERT的问题
  17. hadoop开发环境
  18. 王者荣耀交流协会 - 第6次Scrum会议
  19. 【Nginx】修改响应头,根据不同请求IP重定向到不同IP
  20. python 动态语言 __slots__

热门文章

  1. python获取命令行参数的方法(汇总)
  2. 团队作业(五)-笔记app top5
  3. 第十周PSP&amp;进度条
  4. no-referrer-when-downgrade什么意思
  5. java之常量折叠
  6. delphi Timage 加上滚动条方法
  7. Spring之使用表达式配置切入点
  8. 一本通1639Biorhythms
  9. 关于django-rest-freamwork中的View
  10. subprocess 子进程模块