MEDIUM Greedy

452. Minimum Number of Arrows to Burst Balloons

๐Ÿ“– Problem

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, ystart, xend, yend] denotes a balloon whose horizontal diameter stretches between xstart and xend and vertical diameter stretches between ystart and yend. An arrow can be shot at exactly xstart if ystart <= y <= yend along the path the arrow travels. No two arrow travel along exactly the same path. Return the minimum number of arrows that must be shot to burst all balloons.

๐Ÿง  Visual Learning Aid

1 Model the input into the right structure
2 Choose the core technique and invariant
3 Execute step-by-step with a sample
4 Validate complexity and edge cases

JS/TS Refreshers

  • โ€ขArray methods (`push`, `pop`, `shift`, `slice`)
  • โ€ขObject/Map/Set usage patterns
  • โ€ขFunction parameter and return typing

Logical Thinking Concepts

  • โ€ขDefine invariants before coding
  • โ€ขCheck edge cases first (`[]`, single element, duplicates)
  • โ€ขEstimate time/space before implementation
  • โ€ขApply Greedy reasoning pattern

๐Ÿ’ก Approach

  • โ†’ Sort balloons by end coordinate
  • โ†’ Count overlapping intervals greedily
  • โ†’ If current balloon starts after previous ends, need new arrow
  • โ†’ Time: O(n log n), Space: O(1)

๐Ÿงญ Prerequisites

๐Ÿ› ๏ธ Hints & Pitfalls

Hints

  • โ€ขSort balloons by end coordinate
  • โ€ขCount overlapping intervals greedily
  • โ€ขIf current balloon starts after previous ends, need new arrow

Common Pitfalls

  • โ€ขTime: O(n log n), Space: O(1)

๐Ÿงช Test Cases

Hidden tests on submit: 1

Test Case 1
Not run
Input:
findMinArrowShots([[10,16],[2,8],[1,6],[7,12]]);
Expected:
2
Test Case 2
Not run
Input:
findMinArrowShots([[1,2],[3,4],[5,6],[7,8]]);
Expected:
4
Test Case 3
Not run
Input:
findMinArrowShots([[1,2],[2,3],[3,4],[4,5]]);
Expected:
2

๐Ÿ“ Code Editor

๐Ÿ“š Reference Solution

โ–ผ
โŒ˜K Search โŒ˜โ†ฉ Run โŒ˜S Submit