Problem overview
The method findMinArrowShots receives a list points. Each pair [start, end] is one balloon that covers every point on a line from its start to its end. An arrow shot at the point x bursts every balloon with start <= x <= end. The method returns the fewest arrows that burst all the balloons. Balloons may overlap, and one arrow can burst several of them.
For [[-4, 2], [0, 6], [5, 9], [1, 3], [8, 8]] the answer is 2. An arrow at x = 2 bursts [-4, 2], [0, 6] and [1, 3], and an arrow at x = 8 bursts [5, 9] and [8, 8]. For [[0, 20], [3, 4], [6, 8], [15, 30]] the answer is 3: the balloons [3, 4], [6, 8] and [15, 30] share no point, so each needs its own arrow, while [0, 20] is burst by one of those arrows. For [[1, 3], [3, 6]] the answer is 1, since both balloons cover the point 3.
The lesson asks for O(n log n) time. There are up to 100,000 balloons, in any order, with ends anywhere from about minus 2 billion to 2 billion.
Approach: sort by end and shoot at the first end
Looking for the best arrow positions on a line of billions of points would be hopeless, but only a few positions matter. Take the balloon that ends first. Any arrow that bursts it flies at its end or earlier. Moving that arrow right, to the balloon's end, loses nothing: every other balloon that the earlier position would burst starts at or before that position, and ends at or after the first end, because no balloon ends sooner. So it contains the end point as well.
So shoot the first arrow at the smallest end. It bursts every balloon that starts at or before that point. For the balloons left, repeat: take the one that ends first, and shoot there.
Sorting by end makes this one pass. After the sort, a balloon that starts at or before the last arrow's position arrowX also ends at or after it, because the ends are sorted, so the last arrow already bursts it. A balloon that starts after arrowX is out of reach of every arrow so far, and it needs a new one, shot at its own end.
Step by step
- Sort
pointsby end:points.sort(key=lambda p: p[1]). Thekeyfunction receives a pairpand returns what to sort by, here its second element. - Create
arrows = 1. There is at least one balloon, so at least one arrow is needed. - Create
arrowX = points[0][1], the end of the first balloon after the sort. - Loop with
balloonoverpoints. The first balloon is visited too; it starts at or beforearrowX, so nothing changes. - If
balloon[0] > arrowX, the last arrow misses it. Use>, not>=: a balloon that starts exactly atarrowXis burst, as in[[1, 3], [3, 6]]. - Add 1 to
arrows. All earlier arrows flew atarrowXor before. - Set
arrowX = balloon[1]. Of the balloons still standing, this one ends first, so its end is the best spot. - Return
arrows.
On the first example, sorting by end gives this order:
[[-4, 2], [1, 3], [0, 6], [8, 8], [5, 9]]
The first arrow goes to x = 2. The balloons [1, 3] and [0, 6] start at 1 and 0, both at most 2, so they are already burst. The balloon [8, 8] starts at 8, which is greater than 2, so a second arrow goes to x = 8. The last balloon, [5, 9], starts at 5, at most 8, so it is burst. The answer is 2.
The solution in Python
class Solution:
def findMinArrowShots(self, points: List[List[int]]) -> int:
points.sort(key=lambda p: p[1])
arrows = 1
arrowX = points[0][1]
for balloon in points:
if balloon[0] > arrowX:
arrows += 1
arrowX = balloon[1]
return arrows
Python details:
list.sort()sorts in place and returnsNone. That is why the code calls it on its own line and then keeps usingpoints. Writingpoints = points.sort(...)would makepointsequal toNone. The in-place sort also reorders the caller's list. For the first example, the input list ends up in the order shown above.- The sort is stable and compares only the ends, so balloons with the same end keep their relative order. The order among them does not change the count.
- Python integers have no fixed size, so ends near plus or minus 2 billion need no special care.
Complexity
Time: O(n log n). The sort takes O(n log n), and the loop adds one O(n) pass.
Space: O(1) extra, apart from what the sort uses internally, since the code only keeps arrows and arrowX.
Edge cases and common mistakes
- A single balloon.
[[-2147483648, 2147483647]]returns 1. - Touching balloons.
[[1, 3], [3, 6]]returns 1 because of the strict>. With>=the answer would be 2. - Balloons that don't overlap at all.
[[1, 2], [3, 4], [5, 6]]returns 3: each one needs its own arrow. - A balloon inside another.
[[1, 5], [2, 3], [4, 9]]returns 2: the arrow at 3 bursts[2, 3]and[1, 5], and[4, 9]needs the second. - Sorting by start instead of end. Then the first balloon may stretch far to the right, and shooting at its end could miss balloons that a shot further left would have burst. The end is the right key.
Testing
Results from running the method:
| points | Result |
|---|---|
[[-4, 2], [0, 6], [5, 9], [1, 3], [8, 8]] | 2 |
[[0, 20], [3, 4], [6, 8], [15, 30]] | 3 |
[[1, 3], [3, 6]] | 1 |
[[1, 2], [3, 4], [5, 6]] | 3 |
[[-2147483648, 2147483647]] | 1 |
[[1, 5], [2, 3], [4, 9]] | 2 |
