lintcode-medium-Number of Airplanes in the Sky
2016-04-02 07:38
447 查看
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
Return
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
/** * Definition of Interval: * public classs Interval { * int start, end; * Interval(int start, int end) { * this.start = start; * this.end = end; * } */ class Solution { /** * @param intervals: An interval array * @return: Count of airplanes are in the sky. */ public int countOfAirplanes(List<Interval> airplanes) { // write your code here if(airplanes == null || airplanes.size() == 0) return 0; ArrayList<point> list = new ArrayList<point>(); for(Interval interval: airplanes){ list.add(new point(interval.start, 1)); list.add(new point(interval.end, 0)); } Collections.sort(list, new Comparator<point>(){ public int compare(point p1, point p2){ if(p1.time == p2.time){ return p1.flag - p2.flag; } else{ return p1.time - p2.time; } } }); int count = 0; int ans = 0; for(point p: list){ if(p.flag == 1) count++; else count--; ans = Math.max(ans, count); } return ans; } class point{ int time; int flag; public point(int time, int flag){ this.time = time; this.flag = flag; } } }
相关文章推荐
- 使用MacBook Air的4项基本技巧
- Xenomai on the Beaglebone Black in 14 easy steps ---fwqlzz love is for ever
- 11. Container With Most Water
- 阶梯博弈(Stair Nim)
- leetcode 172. Factorial Trailing Zeroes
- rails中使用ajax
- 领域驱动设计(Domain Driven Design)参考架构详解
- leetcode 每日一题 172. Factorial Trailing Zeroes
- tail -F查看线上日志
- Run-Time Check Failure #0 错误
- leetcode 70. Climbing Stairs
- 【HDU】 1023 Train Problem II
- 解决maillog文件删除后无法重建的问题
- opencv_traincascade问题
- AIDL的Binder框架
- 出现( linker command failed with exit code 1)错误总结
- 【HDU】 1022 Train Problem I
- 手动执行failover
- 朴素贝叶斯(naive bayes)
- SendMail发邮件