Minimum Arrows to Burst Every Balloon

Quick Overview

Find the fewest vertical arrows needed to burst every balloon, including balloons whose horizontal intervals overlap or touch at an endpoint.

Minimum Arrows to Burst Every Balloon

Company: Barclays

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Minimum Arrows to Burst Every Balloon Balloons are attached to a wall. Each balloon is described by its horizontal diameter: `points[i] = [start, end]`. Its vertical position is unknown. An arrow fired vertically upward from horizontal coordinate `x` travels indefinitely and bursts every balloon whose interval contains `x`. Both endpoints count: the balloon bursts exactly when `start <= x <= end`. You may fire as many arrows as needed, at any horizontal coordinates. Return the minimum number of arrows needed to burst all balloons. Return only the count, not the firing coordinates. ### Function Interface ```cpp class Solution { public: int findMinArrowShots(vector<vector<int>>& points); }; ``` ### Examples **Example 1** ```text Input: points = [[1,4],[4,7],[8,10],[9,12]] Output: 2 ``` An arrow at `x = 4` bursts the first two balloons, including their shared endpoint. Another at `x = 9` bursts the last two. One arrow cannot reach both `[1,4]` and `[9,12]`, so two is the minimum. **Example 2** ```text Input: points = [[-10,10],[-5,0],[0,5]] Output: 1 ``` All three balloons contain `x = 0`, so one arrow bursts all of them. ### Constraints - `1 <= points.size() <= 100000`. - Every `points[i]` has exactly two entries. - `-2147483648 <= start < end <= 2147483647`. ### Clarifications - One arrow can burst any number of balloons along its vertical path. - Intervals may overlap, contain one another, or be identical. - Touching at a single endpoint is enough for two balloons to share an arrow. - The unknown vertical positions place no additional restriction on a shot. ### What a Strong Answer Covers - The minimum count for all balloons, with a justification of optimality. - Correct handling of shared endpoints, nested intervals, and repeated intervals. - An implementation suitable for `100000` intervals and the entire signed 32-bit endpoint range.

Overview: Find the fewest vertical arrows needed to burst every balloon, including balloons whose horizontal intervals overlap or touch at an endpoint.

Barclays logo
Barclays
mediumSoftware EngineerOtherCoding & Algorithms
1
0

Minimum Arrows to Burst Every Balloon

Balloons are attached to a wall. Each balloon is described by its horizontal diameter: points[i] = [start, end]. Its vertical position is unknown.

An arrow fired vertically upward from horizontal coordinate x travels indefinitely and bursts every balloon whose interval contains x. Both endpoints count: the balloon bursts exactly when start <= x <= end. You may fire as many arrows as needed, at any horizontal coordinates.

Return the minimum number of arrows needed to burst all balloons. Return only the count, not the firing coordinates.

Function Interface

class Solution {
public:
    int findMinArrowShots(vector<vector<int>>& points);
};

Examples

Example 1

Input: points = [[1,4],[4,7],[8,10],[9,12]]
Output: 2

An arrow at x = 4 bursts the first two balloons, including their shared endpoint. Another at x = 9 bursts the last two. One arrow cannot reach both [1,4] and [9,12], so two is the minimum.

Example 2

Input: points = [[-10,10],[-5,0],[0,5]]
Output: 1

All three balloons contain x = 0, so one arrow bursts all of them.

Constraints

  • 1 <= points.size() <= 100000 .
  • Every points[i] has exactly two entries.
  • -2147483648 <= start < end <= 2147483647 .

Clarifications

  • One arrow can burst any number of balloons along its vertical path.
  • Intervals may overlap, contain one another, or be identical.
  • Touching at a single endpoint is enough for two balloons to share an arrow.
  • The unknown vertical positions place no additional restriction on a shot.

What a Strong Answer Covers Guidance

  • The minimum count for all balloons, with a justification of optimality.
  • Correct handling of shared endpoints, nested intervals, and repeated intervals.
  • An implementation suitable for 100000 intervals and the entire signed 32-bit endpoint range.