Skip to content

Commit f898842

Browse files
committed
D. J.:
- Added the leetcode problem and solution for 452 Minimum Number of Arrows to Burst Balloons
1 parent 34b4790 commit f898842

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ This repository contains awesome LeetCode problems and solutions written in Pyth
4848
- [290 Word Pattern](https://leetcode.com/problems/word-pattern/description/)
4949
- [383 Ransom Note](https://leetcode.com/problems/ransom-note/description/)
5050
- [392 Is Subsequence](https://leetcode.com/problems/is-subsequence/description/)
51+
- [452 Minimum Number of Arrows to Burst Balloons](https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/description/)
5152

5253
## Development 🔧
5354

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def findMinArrowShots(self, points: List[List[int]]) -> int:
6+
"""
7+
There are some spherical balloons taped onto a flat wall that represents the XY-plane.
8+
The balloons are represented as a 2D integer array points where points[i] = [xstart, xend]
9+
denotes a balloon whose horizontal diameter stretches between xstart and xend.
10+
You do not know the exact y-coordinates of the balloons.
11+
12+
Arrows can be shot up directly vertically (in the positive y-direction) from different
13+
points along the x-axis. A balloon with xstart and xend is burst by an arrow shot at
14+
x if xstart <= x <= xend. There is no limit to the number of arrows that can be shot.
15+
A shot arrow keeps traveling up infinitely, bursting any balloons in its path.
16+
17+
Given the array points, return the minimum number of arrows that must be shot to burst all balloons.
18+
"""
19+
points = sorted(points, key=lambda p: p[0])
20+
l, r = points[0][0], points[0][1]
21+
i, output = 1, 1
22+
while i < len(points):
23+
c_l, c_r = points[i][0], points[i][1]
24+
new_l, new_r = max(l, c_l), min(r, c_r)
25+
if new_l <= new_r:
26+
l, r = new_l, new_r
27+
else:
28+
l, r = c_l, c_r
29+
output += 1
30+
i += 1
31+
return output
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
from awesome_python_leetcode._452_minimum_number_of_arrows_to_burst_balloons import (
6+
Solution,
7+
)
8+
9+
10+
@pytest.mark.parametrize(
11+
argnames=["points", "expected"],
12+
argvalues=[
13+
([[10, 16], [2, 8], [1, 6], [7, 12]], 2),
14+
([[1, 2], [3, 4], [5, 6], [7, 8]], 4),
15+
([[1, 2], [2, 3], [3, 4], [4, 5]], 2),
16+
],
17+
)
18+
def test_func(points: List[List[int]], expected: int):
19+
min_arrow_shots = Solution().findMinArrowShots(points)
20+
assert min_arrow_shots == expected

0 commit comments

Comments
 (0)