给定一个区间的集合,找到需要移除区间的最小数量,使剩余区间互不重叠。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/non-overlapping-intervals
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
import java.util.Arrays;
import java.util.Comparator;
class Solution {
public int eraseOverlapIntervals(int[][] intervals) {
if (intervals == null || intervals.length == 0) {
return 0;
}
int ans = 0;
Arrays.sort(intervals, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return Integer.compare(o1[1], o2[1]);
}
});
int right = intervals[0][0];
for (int[] interval : intervals) {
if (interval[0] >= right) {
ans++;
right = interval[1];
}
}
return intervals.length - ans;
}
}