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.
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
本文介绍了一种使用扫线算法解决的问题:给定飞行和降落时间的区间列表,如何计算最多有多少架飞机同时在空中。算法通过分别保存起飞和降落时间,排序后遍历并更新当前空中的飞机数量,最终返回最大数量。该问题考虑了同时起飞和降落的情况,确保算法正确处理。通过实例演示了解决步骤,并提供了关键代码实现。
898

被折叠的 条评论
为什么被折叠?



