Skip to content

Commit fdfb168

Browse files
committed
"Minimum Number of Arrows to Burst Balloons" solution
1 parent 0a17927 commit fdfb168

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def findMinArrowShots(self, points: list[list[int]]) -> int:
3+
assert points
4+
5+
points = sorted(points)
6+
7+
n = len(points)
8+
arrows = 1
9+
_, end = points[0]
10+
11+
for i in range(1, n):
12+
next_start, next_end = points[i]
13+
14+
if next_start > end:
15+
arrows += 1
16+
end = next_end
17+
elif next_end < end:
18+
end = next_end
19+
20+
return arrows
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import pytest
2+
3+
from src.minimum_number_of_arrows_to_burst_balloons import Solution
4+
5+
6+
@pytest.mark.parametrize(
7+
"points,expected",
8+
(
9+
([[10, 16], [2, 8], [1, 6], [7, 12]], 2),
10+
([[1, 2], [3, 4], [5, 6], [7, 8]], 4),
11+
([[1, 2], [2, 3], [3, 4], [4, 5]], 2),
12+
),
13+
)
14+
def test_solution(points, expected):
15+
assert Solution().findMinArrowShots(points) == expected

0 commit comments

Comments
 (0)