[LintCode] Number of Airplanes in the Sky

Posted Push your limit!

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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

 

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

lintcode-medium-Number of Airplanes in the Sky

[LintCode] Number of Airplanes in the Sky

[LintCode] Number of Islands(岛屿个数)

c_cpp http://www.lintcode.com/en/problem/count-of-smaller-number/

[LintCode] 1835. Number of Ways to Stay in the Same Place After Some Steps I

随手练——LintCode 433 - 小岛数量