Greedy Algorithms8 sections · 316 units
Open in Course

Arrows - Implementation

The code

Here is the solution:

function findMinArrowShots(points)
 if points is empty then
 return 0
 sort points by end coordinate
 arrows := 1
 arrowPos := points[0].end
 for i from 1 to length - 1
 if points[i].start > arrowPos then
 arrows := arrows + 1
 arrowPos := points[i].end
 return arrows

Time: O(nlogn)O(n \log n). Space: O(1)O(1).

If the current balloon starts after our arrow position, we need a new arrow. Otherwise, the current arrow already bursts it.