思路
代码
var eraseOverlapIntervals = function(intervals) {
if(intervals.length==0) return 0;
intervals.sort(function(a, b){
return a[1]-b[1];
})
let count = 1;
let end = intervals[0][1]
for(let i=1; i<intervals.length; i++){
if(intervals[i][0]>=end){
count = count + 1;
end = intervals[i][1];
}
}
return intervals.length - count;
};
思路
与上面思路一样,此题可以按照终点排序,也可以按照起点排序,目的是让球聚集。
代码
var findMinArrowShots = function(points) {
if(points.length==0) return 0;
points.sort(function(a, b){
return a[0]-b[0];
})
let count = 1;
let minend = points[0][1];
for(let i=1; i<points.length; i++){
if(points[i][0]>minend){
count = count + 1;
minend = points[i][1];
}else if(points[i][1]<minend){
minend = points[i][1];
}
}
return count;
};
记住这张图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/5ec2c75763c2a570616a818f4dadfb8e.png)