zoukankan      html  css  js  c++  java
  • [LintCode] 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?

    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

    This problem is a classical example of applying sweep-line algorithm. Typically each interval represents one event, 

    the start of an interval represents the beginning of an event and the end of the interval represents the finish of the

    same event. In this problem, interval's start means one airplane takes off; interval's end means the same airplane lands.

    Algorithm: 

    1. save flying times and landing time separately and sort them in ascending order.

    2. Iterate through all flying times and do the following. (only flying time may give us more airplanes in the sky)

     a. If the current earliest flying time is smaller than the current earliest landing time, we know we have 1 more 

      plane flying, 0 more plane landing.  Increment current number of planes in the sky by 1 and set current fly time 

      to the next earliest time.

     b. else, we know we have 1 more plane landing, 0 more plane flying, Decrement current number of planes in the sky

      by 1 and set current land time to the next earliest time.

     c. After processing the current flying time of either case a or b, update the max number.

    Special case to consider: What about if one plane flys and another plane lands at the same time? Does the above algorithm 

    still work?

    When starts[startIdx] == ends[endIdx],  the above algorithm consider it as 1 plane landing, so curr--, endIdx++; 

    But startIdx is not changed and the while loop exit condition is startIdx >= n, so we'll still process this fly time in the next iteration. 

    It will always be processed as the latest landing time must be the biggest number of all. So for each new fly time, case a always 

    hold once. This proves the above algorithm works for this special case, it even works for cases where we have multiple planes 

    fly or land at the same time.

     1 /**
     2  * Definition of Interval:
     3  * public classs Interval {
     4  *     int start, end;
     5  *     Interval(int start, int end) {
     6  *         this.start = start;
     7  *         this.end = end;
     8  *     }
     9  */
    10 
    11 class Solution {
    12     /**
    13      * @param intervals: An interval array
    14      * @return: Count of airplanes are in the sky.
    15      */
    16     public int countOfAirplanes(List<Interval> airplanes) { 
    17         if(airplanes == null) {
    18             return 0;    
    19         }    
    20         if(airplanes.size() <= 1) {
    21             return airplanes.size();
    22         }
    23         int n = airplanes.size();
    24         int[] starts = new int[n];
    25         int[] ends = new int[n];
    26         for(int i = 0; i < n; i++){
    27             starts[i] = airplanes.get(i).start;
    28             ends[i] = airplanes.get(i).end;
    29         }
    30         Arrays.sort(starts);
    31         Arrays.sort(ends);
    32         int startIdx = 0, endIdx = 0, curr = 0, max = 0;
    33         while(startIdx < n){
    34             if(starts[startIdx] < ends[endIdx]){
    35                 curr++;
    36                 startIdx++;
    37             }
    38             else{
    39                 curr--;
    40                 endIdx++;
    41             }
    42             max = Math.max(max, curr);
    43         }
    44         return max;
    45     }
    46 }

    Related Problems 

    Merge Intervals

  • 相关阅读:
    ASP.NET在访问Controller的方法带参数时怎样防止黑客攻击
    ASP.NET项目在VS中F5与Ctrl+F5的区别
    ASP.NET中MVC添加Controller以及访问其Action
    ASP.NET中MVC编程模式简介与搭建HelloWorld项目
    搭建自己的博客(三十):添加修改密码,忘记密码
    pycharm破解
    安装更新npm和nodejs
    搭建自己的博客(二十九):增加绑定邮箱的功能,完善用户信息
    格式化注释
    搭建自己的博客(二十八):自定义用户模型
  • 原文地址:https://www.cnblogs.com/lz87/p/7181301.html
Copyright © 2011-2022 走看看