Q452 Minimum Number of Arrows to Burst Balloons
1. Sort by start point then end point, we might get the sorted ballons as below
---------
------
---------
------
---------
2. In order to sort as more balloons as possible, try to shot at the end point
starting at the endpoint of ballon 1, record it as the arrow
check the next ballon, if we find the start point is smaller than arrow which means we can try to adjust the arrow to shot it by this arrow, by min(endpoint, arrow)
(In the above example, the first shot would be at the end point of 2th balloon, at shot 1,2,3 together, since the start point of 4th is greater than the arrow. So once we shot 1-3, we have to do another shot for the 4th, update the count.)
And we have to repeat the above steps from 4th balloon, assume the next shot is at endpoint of 4th
https://github.com/Sylvia-YiyinShen/Algorithm/blob/master/Greedy/Q452MinArrows.playground/Contents.swift
No comments:
Post a Comment