• [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

  • 相关阅读:
    二叉树的先,中,后序遍历
    MacOS删除iCloud云盘文件但未释放空间解决办法
    月更24/30
    iPod shuffle(第 4 代)修改VoiceOver播放语言
    在win10上安装face_recognition(人脸识别)
    FFT模板(Python)
    Huffman树模板(C++)
    如何取消 Fetch 请求 All In One
    互联网术语汇总 All In One
    Vue 3 Migration Guide All In One
  • 原文地址:https://www.cnblogs.com/lz87/p/7181301.html
Copyright © 2020-2023  润新知