-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy path14_countOfAirplanes.java
62 lines (55 loc) · 1.53 KB
/
14_countOfAirplanes.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
public class Interval {
int start, end;
Interval(int start, int end) {
this.start = start;
this.end = end;
}
class Solution {
/**
* @param intervals: An interval array
* @return: Count of airplanes are in the sky.
*/
public int countOfAirplanes(List<Interval> airplanes) {
// write your code here
if(airplanes==null)
return 0;
if(airplanes.size()==0)
return 0;
int maxTime = getTheMaxEndTime(airplanes);
int[] airTimeArray = new int[maxTime];
for(int i=0; i<airplanes.size(); i++){
Interval tmp = airplanes.get(i);
airTimeArray[tmp.start-1]++;
airTimeArray[tmp.end-1]--;
}
return maxSumOfSubArray(airTimeArray);
}
public int maxSumOfSubArray(int[] array){
int curSum=array[0];
int maxSum=array[0];
for(int i=1; i<array.length; i++){
if(curSum<0)
curSum = array[i];
else curSum += array[i];
if(curSum>maxSum)
maxSum = curSum;
}
return maxSum;
}
public int getTheMinStartTime(List<Interval> airplanes){
int min=list.get(0).start;
for(int i=1; i<airplanes.size(); i++){
if(airplanes.get(i).start<min)
min = airplanes.get(i).start;
}
return min;
}
public int getTheMaxEndTime(List<Interval> airplanes){
int max = list.get(0).end;
for(int i=1; i<airplanes.size(); i++){
if(airplanes.get(i).end>max)
max = airplanes.get(i).end;
}
return max;
}
}