AlgoMaster Logo

Minimum Number of Arrows to Burst Balloons

points=[[10,16],[2,8],[1,6],[7,12]]
1public int findMinArrowShots(int[][] points) {
2    Arrays.sort(points, (a, b) -> Integer.compare(a[1], b[1]));
3    int arrows = 1;
4    int currentEnd = points[0][1];
5    for (int i = 1; i < points.length; i++) {
6        int[] currentBalloon = points[i];
7        if (currentBalloon[0] <= currentEnd) {
8            // Current arrow can burst this balloon
9            continue;
10        } else {
11            // Need a new arrow
12            arrows++;
13            currentEnd = currentBalloon[1];
14        }
15    }
16    return arrows;
17}
0 / 9
[10,16][2,8][1,6][7,12]burst