Open youngyangyang04 opened 3 months ago
Arrays.sort(points, (a, b) -> Integer.compare(a[0], b[0]));
int count = 1, end = points[0][1];
for (int i = 1; i < points.length; i++) {
if (points[i][0] > end) {
count++;
end = points[i][1];
} else {
end = Math.min(end, points[i][1]);
}
}
return count;
// 例: points = [[1,6],[2,8],[7,12],[10,16]], count = 1, end = 6
i = 1: 不满足 2 > 6(射得到), 则 end = min(6, 8) = 6
i = 2: 满足 7 > 6(射不到), 则 count = 2, end = 12
i = 3: 不满足 10 > 12(射得到), 则 end = min(12, 16) = 6
巧妙!
https://www.programmercarl.com/0452.%E7%94%A8%E6%9C%80%E5%B0%91%E6%95%B0%E9%87%8F%E7%9A%84%E7%AE%AD%E5%BC%95%E7%88%86%E6%B0%94%E7%90%83.html