https://leetcode.cn/problems/non-overlapping-intervals/
题目要求
给定一个区间的集合 intervals ,其中 intervals[i] = [starti, endi] 。返回 需要移除区间的最小数量,使剩余区间互不重叠 。
方法
class Solution {
public int eraseOverlapIntervals(int[][] intervals) {
Arrays.sort(intervals, (o1, o2) -> o1[0] == o2[0] ? o1[1] - o2[1] : o1[0] - o2[0]);
int n = intervals.length;
int range = intervals[0][1];
int res = 0;
for (int i = 1; i < n; i++) {
// 区间没有重叠
if (intervals[i][0] >= range) range = intervals[i][1];
// 区间重叠
else {
res++;
range = Math.min(range, intervals[i][1]);
}
}
return res;
}
}