lintcode-medium-Number of Airplanes in the Sky

Posted 哥布林工程师

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了lintcode-medium-Number of Airplanes in the Sky相关的知识,希望对你有一定的参考价值。

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

 

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

 

以上是关于lintcode-medium-Number of Airplanes in the Sky的主要内容,如果未能解决你的问题,请参考以下文章

在 Dart 中,List.from 和 .of 以及 Map.from 和 .of 有啥区别?

JavaScript `of` 关键字(for...of 循环)

nth-child,nth-last-child,only-child,nth-of-type,nth-last-of-type,only-of-type,first-of-type,last-of-

be aware of是啥意思

be aware of是啥意思

全文搜索条件 'control of' 中的 'of' 附近的语法错误