AC代码
/**
* 经典贪心题目,挑选结束时间早的(放到这题就是挑选右区间更小的),这样能尽可能多的选择任务数量
* (放到这题就是能选择更多的不重叠的区间)
*/
public int eraseOverlapIntervals(int[][] intervals) {
if(intervals == null || intervals.length <= 0) return 0;
Arrays.sort(intervals, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o1[1] -o2[1];
}
});
int sum = 1;
int leftTemp;
int rightTemp = intervals[0][1];
for (int i = 0;i < intervals.length; i++) {
leftTemp = intervals[i][0];
if (leftTemp >= rightTemp ) {
rightTemp = intervals[i][1];
sum++;
}
}
return intervals.length - sum;
}