码迷,mamicode.com
首页 > 其他好文 > 详细

[LintCode] Number of Airplanes in the Sky

时间:2017-07-15 09:58:01      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:int end   ane   pre   problems   air   following   lint   start   app   

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

标签:int end   ane   pre   problems   air   following   lint   start   app   

原文地址:http://www.cnblogs.com/lz87/p/7181301.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!