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
๐ค Output