<AI/>
enru
Все статьи

Minimum number of arrows to burst balloons

LeetCode 452: Find the minimum number of arrows needed to burst all balloons by sorting them by their right edge.

Сложность
Средняя
Опубликовано
Темы
#array#greedy
Открыть на LeetCode

Статья пока доступна только на английском.

Problem Description

There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array points, where points[i] = [xstart, xend] denotes a balloon whose horizontal diameter stretches between xstart and xend. You don't know the exact Y coordinates of the balloons.

Arrows can be shot straight up (in the positive Y direction) from different points along the X axis. A balloon with xstart and xend is burst by an arrow shot at x if xstart <= x <= xend. There is no limit on the number of arrows that can be shot.

A shot arrow keeps travelling up infinitely, bursting every balloon in its path. Find the minimum number of arrows that must be shot to burst all balloons.

Constraints

  • points contains from 1 to 105 elements
  • Coordinates satisfy -231 <= xstart < xend <= 231 - 1

Examples

Input: points = [[10,16],[2,8],[1,6],[7,12]]

Answer: 2

Explanation

The balloons can be burst with two arrows:

  • Shoot an arrow at x = 6, bursting the balloons [2, 8] and [1, 6].
  • Shoot an arrow at x = 11, bursting the balloons [10, 16] and [7, 12].

Solution

x 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 [1, 6] [2, 8] [7, 12] [10, 16] arrow 1: x = 6 arrow 2: x = 12

The first step is to sort points by the right edge of the balloons. This sorting makes it easy to figure out how many arrows we need to burst all the balloons.

If the balloons are sorted by their end coordinate, the next balloon can only be in one of two positions.

  • Its start coordinate is greater than the end coordinate of the current balloon. In this case the balloons don't overlap and cannot be burst with a single arrow.
  • Its start coordinate is less than or equal to the end coordinate of the current balloon. In this case the balloons overlap and can be burst with a single arrow shot at the end coordinate of the current balloon.

This gives us a very simple algorithm:

  • Iterate over all balloons in the sorted array.
  • Remember the coordinate of the last shot, which is initially set to the smallest possible value.
  • If the start coordinate of the current balloon is greater than the coordinate of the last shot, we need to shoot at the right edge of this balloon and increment the arrow counter by 1.
  • If the start coordinate of the current balloon is less than or equal to the coordinate of the last shot, the balloon overlaps with the previous one and no new shot is needed, since it has already been burst by the previous arrow.
typescript
export const findMinArrowShots = (points: number[][]): number => {
  // Sort the points by the second coordinate of each pair
  points.sort((a, b) => a[1] - b[1]);
 
  let count = 0;
  let lastShot = -Infinity;
 
  for (let cords of points) {
    if (cords[0] > lastShot) {
      count++;
      lastShot = cords[1];
    }
  }
 
  return count;
};

Complexity Analysis

Time Complexity

O(n log n), because of sorting points.

Space Complexity

Varies from O(log n) to O(n) depending on the sorting algorithm implementation. Otherwise, memory usage is constant.