public int countOfAirplanes(List<Interval> airplanes) {
if (airplanes == null || airplanes.size() == 0) {
return 0;
}
int last = 0;
for (Interval interval : airplanes) {
last = Math.max(last, interval.end);
}
int[] res = new int[last + 2];
for (Interval in : airplanes) {
res[in.start]++;
res[in.end]--;
}
int max = 0;
int runningSum = 0;
for (int i = 1; i < last + 2; i++) {
runningSum += res[i];
max = Math.max(runningSum, max);
}
return max;
}
class Point{
int time;
int flag;
Point(int t, int s){
this.time = t;
this.flag = s;
}
public static Comparator<Point> PointComparator = new Comparator<Point>(){
public int compare(Point p1, Point p2){
if(p1.time == p2.time) return p1.flag - p2.flag;
else return p1.time - p2.time;
}
};
}
class Solution {
/**
* @param intervals: An interval array
* @return: Count of airplanes are in the sky.
*/
public int countOfAirplanes(List<Interval> airplanes) {
List<Point> list = new ArrayList<>(airplanes.size()*2);
for(Interval i : airplanes){
list.add(new Point(i.start, 1));
list.add(new Point(i.end, 0));
}
Collections.sort(list,Point.PointComparator );
int count = 0, ans = 0;
for(Point p : list){
if(p.flag == 1) count++;
else count--;
ans = Math.max(ans, count);
}
return ans;
}
}