Greedy Algorithms8 sections · 316 units
Open in Course

Minimum Arrows - Implementation

The code

function findMinArrowShots(points)
 if length of points = 0 then
 return 0
 sort points by end ascending
 arrows := 1
 arrowPos := points[0].end
 for i from 1 to length of points - 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) extra.