LintCode :数飞机
来源:互联网 发布:淘宝金牌卖家有哪些 编辑:程序博客网 时间:2024/04/28 22:40
数飞机
给出飞机的起飞和降落时间的列表,用 interval 序列表示. 请计算出天上同时最多有多少架飞机?
您在真实的面试中是否遇到过这个题?
Yes
样例
对于每架飞机的起降时间列表:[[1,10],[2,3],[5,8],[4,7]]
, 返回3
。
注意
标签 Expand 如果多架飞机降落和起飞在同一时刻,我们认为降落有优先权。
相关题目 Expand
解题思路:
用一个map记录每一时间点的降落和起飞的飞机数
比如[1,10] 用map记录为map[ 1 ]++1时刻多了一只飞机,map[ 10 ]--,10时刻少了一只飞机
/*** 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; Map<Integer, Integer> map = new HashMap<>(); int len = airplanes.size(); Set<Integer> set = new TreeSet<>(); for(int i=0;i<len;i++){ int s = airplanes.get(i).start; int e = airplanes.get(i).end; map.put(s,map.containsKey(s)?map.get(s)+1:1); map.put(e,map.containsKey(e)?map.get(e)-1:-1); set.add(s); set.add(e); } int maxAirNo = Integer.MIN_VALUE; int curcount = 0; Iterator<Integer> tmp = set.iterator(); while(tmp.hasNext()){ curcount += map.get(tmp.next()); maxAirNo = Math.max(maxAirNo, curcount); } return maxAirNo; }}
0 0
- LintCode-数飞机
- LintCode--数飞机
- lintcode-数飞机-391
- LintCode :数飞机
- Lintcode:数飞机
- Lintcode:数飞机
- lintcode(391)数飞机
- lintcode-数飞机
- 数飞机 -LintCode
- lintcode--数飞机
- LintCode 数飞机的两种解法
- LintCode --number-of-airplanes-in-the-sky(数飞机)
- 数飞机
- Lintcode 两数之和
- LintCode 三数之和
- LintCode-最大数
- LintCode-交错正负数
- LintCode 丑数
- 浅谈Activity的常用Flags
- Android 动画
- ZBar 的静态库制作
- 根据Uri获取图片绝对路径,解决Android4.4以上版本Uri转换
- 找不到该项目无法删除
- LintCode :数飞机
- iOS开发-基础:获取系统当前日期,断点输出时间
- JSON数据格式及解析上
- 网络——实现服务器
- Spring Boot 事务的使用
- 使用stream_context_create远程上传文件
- 2597: 编程题B-选拔飞行员
- Android中TextView的使用详情综述
- Python2.7 读取中文,显示为乱码的问题