Created
April 30, 2020 16:30
-
-
Save liyunrui/41ec49ccf5ad2e258b01d167fbc865a7 to your computer and use it in GitHub Desktop.
leetcode-greedy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def findMinArrowShots(self, points: List[List[int]]) -> int: | |
n = len(points) | |
# edge case | |
if n == 0: | |
return 0 | |
ending_time, ans = float('-inf'), 0 # the earliest end time | |
# sort the balloons by their end x-coordinate | |
for i in sorted(points, key=lambda x: x[1]): | |
start_time = i[0] | |
if start_time > ending_time: | |
# since xstart ≤ x ≤ xend, here we take equal as overlapping | |
ending_time = i[1] | |
else: | |
ans += 1 # number of ballons we need to remove make rest of them non-overlapping | |
return n - ans |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment