leetcode题目链接

这道题思路很简单,就是一个贪心,甚至都不需要合并区间。

开始需要对气球的结束坐标排序一下,然后定义一个end指向当前箭的最远位置。

然后开始遍历数组,如果出现区间起始位置比end大,则说明需要再加一只箭。

最后返回弓箭数量。

代码如下:

public int findMinArrowShots(int[][] points) {
        if (points == null || points.length == 0) {
            return 0;
        }

       // 按气球的结束坐标进行排序
        Arrays.sort(points,(a,b)->Integer.compare(a[1],b[1]));

        int arrows = 1; // 至少需要一支箭
        int end = points[0][1]; // 当前箭能引爆的最远位置

        for(int [] point : points) {
            int start = point[0];
            if(start > end) {
                arrows++;
                end= point[1];
            }
        }

        return arrows;
    }

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部