Open youngyangyang04 opened 3 months ago
Arrays.sort(intervals, (a,b) -> Integer.compare(a[1], b[1]));
int count = 1, end = intervals[0][1];
for (int i = 1; i < intervals.length; i++) {
if (intervals[i][0] >= end) {
end = intervals[i][1];
count++;
}
}
return intervals.length - count;
// 排序后: intervals = [[1,2],[2,3],[1,3],[3,4]], count = 1, end = 2
i = 1: 满足 2 >= 2(不重叠), end = 3, count = 2
i = 2: 不满足 1 >= 3(重叠), 继续遍历
i = 3: 满足 3 >= 3(不重叠), end = 4, count = 3 // 最终返回 4 - 3 = 1
https://www.programmercarl.com/0435.%E6%97%A0%E9%87%8D%E5%8F%A0%E5%8C%BA%E9%97%B4.html