-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path452.minimum-number-of-arrows-to-burst-balloons.py
58 lines (52 loc) · 1.54 KB
/
452.minimum-number-of-arrows-to-burst-balloons.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#
# @lc app=leetcode id=452 lang=python3
#
# [452] Minimum Number of Arrows to Burst Balloons
#
# @lc code=start
# TAGS: Greedy, Sort
# REVIEWME: Greedy
class Solution:
# 424 ms, 825 %. Time: O(NlogN). Space: O(1) if not consider internal memory.
def findMinArrowShots(self, points: List[List[int]]) -> int:
"""
------
--- <- make this the new end.
-----
^
|
shoot
-----
--------
---------
"""
if not points: return 0
points.sort()
arrows = pop_ptr = overlap_ptr = 0
while overlap_ptr < len(points):
if points[overlap_ptr][0] > points[pop_ptr][-1]:
pop_ptr = overlap_ptr
arrows += 1
points[pop_ptr][-1] = min(points[pop_ptr][-1], points[overlap_ptr][-1])
overlap_ptr += 1
return arrows + 1
# 392 ms, 98.46%. From the article. It is also Greedy but from a different perspective.
def findMinArrowShots(self, points: List[List[int]]) -> int:
"""
------
---
---------
------
-----------
---
"""
if not points: return 0
points.sort(key = lambda x : x[1])
arrows = 0
pop_end = points[0][-1]
for start, end in points:
if start > pop_end:
arrows += 1
pop_end = end
return arrows + 1
# @lc code=end