归档

452. 用最少数量的箭引爆气球

452. 用最少数量的箭引爆气球

题目

描述:

有一些球形气球贴在一堵用 XY 平面表示的墙面上。墙面上的气球记录在整数数组 points ,其中points[i] = [x_start, x_end] 表示水平直径在 x_start 和 x_end 之间的气球。你不知道气球的精确 y 坐标。

一支弓箭可以沿着 x 轴从不同点 完全垂直 地射出。在坐标 x 处射出一支箭,若有一个气球的直径的开始和结束坐标为 x_start,x_end, 且满足 x_start ≤ x ≤ x_end,则该气球会被引爆。可以射出的弓箭的数量 没有限制 。弓箭一旦被射出之后,可以无限地前进。

给你一个数组 points ,返回引爆所有气球所必须射出的 最小 弓箭数。

示例 1:

  • 输入: points = [[10,16],[2,8],[1,6],[7,12]]
  • 输出: 2
  • 解释: 气球可以用 2 支箭来爆破:
    • 在 x = 6 处射出箭,击破气球 和。
    • 在 x = 11 处发射箭,击破气球 和。

示例 2:

  • 输入: points = [[1,2],[3,4],[5,6],[7,8]]
  • 输出: 4
  • 解释: 每个气球需要射出一支箭,总共需要 4 支箭。

示例 3:

  • 输入: points = [[1,2],[2,3],[3,4],[4,5]]
  • 输出: 2
  • 解释: 气球可以用 2 支箭来爆破:
    • 在 x = 2 处发射箭,击破气球 和。
    • 在 x = 4 处射出箭,击破气球 和。

提示:

  • 1 <= points.length <= 10^5
  • points[i].length == 2
  • -2^31 <= x_start < x_end <= 2^31 - 1

题解

在最早结束的气球右端射箭

一个气球对应横坐标闭区间 [start,end],箭的位置只要落在这个区间内就能引爆它。同一支箭能引爆包含这个位置的所有气球。先按右端点从小到大排序,考虑尚未覆盖的气球中最早结束的一个,把箭放在它的右端点 r。

为什么可以这样选?任何可行方案都必须有一支箭命中这个气球,设原位置为 p,则 p <= r。把这支箭向右移到 r:其他原本被它命中的未处理气球,左端不大于 p,右端又不小于最早的右端 r,因此仍然包含 r。这个调整不会减少覆盖数量,也不会增加箭数,所以存在采用这个位置的最优方案。

扫描时只记录最后一支箭的位置

保存 currentArrowPos。后续气球的右端不会小于它,因此只要左端也不大于它,这支箭就能命中;如果左端大于它,就必须增加一支箭,放在当前气球右端。端点相等仍能命中,所以新增箭的条件是严格的 >。

例如 [[10,16],[2,8],[1,6],[7,12]],按右端排序后先在 6 射箭,覆盖 [1,6] 和 [2,8];[7,12] 的左端大于 6,新增一箭放在 12,同时覆盖 [10,16],答案为 2。空输入返回 0;非空时先为第一个气球准备一箭。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public int findMinArrowShots(int[][] points) {
    if (points.length == 0) {
        return 0;
    }

    Arrays.sort(points, (a, b) -> Integer.compare(a[1], b[1]));

    int arrows = 1;
    int currentArrowPos = points[0][1];

    for (int i = 1; i < points.length; i++) {
        if (points[i][0] > currentArrowPos) {
            arrows++;
            currentArrowPos = points[i][1];
        }
    }

    return arrows;
}

排序比较器不要写成 a[1] - b[1]:坐标范围是 -2^31 到 2^31 - 1,直接相减可能溢出,导致排序结果错误,应使用 Integer.compare(a[1], b[1])。

时间复杂度是 O(n log n),主要花在排序上,后面的扫描只有 O(n);空间复杂度主要取决于排序实现本身。

排序会重排输入中的区间引用。这里的贪心依据是“把命中最早结束区间的那支箭右移到它的右端点,不会失去其他已覆盖的未处理区间”。其他区间题要用同样的选择规则,需要先确认这个依据成立,不能因为题目里出现了区间就直接套用。

完整示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.util.Arrays;

public class Main {

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

        Arrays.sort(points, (a, b) -> Integer.compare(a[1], b[1]));

        int arrows = 1;
        int currentArrowPos = points[0][1];

        for (int i = 1; i < points.length; i++) {
            if (points[i][0] > currentArrowPos) {
                arrows++;
                currentArrowPos = points[i][1];
            }
        }

        return arrows;
    }

    public static void main(String[] args) {
        int[][] points1 = {{10, 16}, {2, 8}, {1, 6}, {7, 12}};
        System.out.println("示例 1 输入: " + Arrays.deepToString(points1));
        int res1 = findMinArrowShots(points1);
        System.out.println("示例 1 输出: " + res1);
        System.out.println("-----------------------------");

        int[][] points2 = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
        System.out.println("示例 2 输入: " + Arrays.deepToString(points2));
        int res2 = findMinArrowShots(points2);
        System.out.println("示例 2 输出: " + res2);
        System.out.println("-----------------------------");

        int[][] points3 = {{1, 2}, {2, 3}, {3, 4}, {4, 5}};
        System.out.println("示例 3 输入: " + Arrays.deepToString(points3));
        int res3 = findMinArrowShots(points3);
        System.out.println("示例 3 输出: " + res3);
        System.out.println("-----------------------------");

        int[][] points4 = {{-2147483646, -2147483645}, {2147483646, 2147483647}};
        System.out.println("示例 4 输入: " + Arrays.deepToString(points4));
        int res4 = findMinArrowShots(points4);
        System.out.println("示例 4 输出: " + res4);
    }
}

运行结果(OpenJDK 21.0.12):

1
2
3
4
5
6
7
8
9
10
11
示例 1 输入: [[10, 16], [2, 8], [1, 6], [7, 12]]
示例 1 输出: 2
-----------------------------
示例 2 输入: [[1, 2], [3, 4], [5, 6], [7, 8]]
示例 2 输出: 4
-----------------------------
示例 3 输入: [[1, 2], [2, 3], [3, 4], [4, 5]]
示例 3 输出: 2
-----------------------------
示例 4 输入: [[-2147483646, -2147483645], [2147483646, 2147483647]]
示例 4 输出: 2
本文由作者按照 CC BY 4.0 进行授权