Swipe Line
391t. Number of Airplanes in the Sky
/**
* Definition of Interval:
* public classs Interval {
* int start, end;
* Interval(int start, int end) {
* this.start = start;
* this.end = end;
* }
* }
*/
class Point{
public int time;
public int flag;
public Point(int time, int flag){
this.time = time;
this.flag = flag;
}
}
public class Solution {
/**
* @param airplanes: An interval array
* @return: Count of airplanes are in the sky.
*/
public int countOfAirplanes(List<Interval> airplanes) {
List<Point> timeline = new ArrayList<>();
for(Interval i: airplanes){
// 若時間相同,先計算降落的飛機,故擺0
timeline.add(new Point(i.start, 1));
timeline.add(new Point(i.end, 0));
}
Collections.sort(timeline, (a,b) ->{
if(a.time == b.time) return a.flag - b.flag;
else return a.time - b.time;}
);
int max=0;
int count=0;
for(Point p: timeline){
if(p.flag == 1) count++;
else count--;
max = Math.max(max, count);
}
return max;
}
}Cheapest Price In Time Intervals
Last updated