Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 96523e4

Browse files
🐱(greedy): 452. 用最少数量的箭引爆气球
1 parent 7222c68 commit 96523e4

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

‎docs/algorithm/greedy/README.md‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,39 @@ class Solution:
268268
return people
269269
```
270270

271+
## 452. 用最少数量的箭引爆气球
272+
273+
[原题链接](https://leetcode-cn.com/problems/minimum-number-of-arrows-to-burst-balloons/)
274+
275+
### 思路
276+
277+
1.`points``begin` 进行升序排序
278+
2. 循环遍历 `points` 判断相邻位置的区间是否发生重叠
279+
- 如果重叠:说明只需要一支箭就可以射穿,并把重叠区间的 `end` 值记录下来,用于比较下一次的区间重叠情况
280+
- 如果不重叠:需要射出的箭数量 + 1
281+
282+
```python
283+
class Solution:
284+
def findMinArrowShots(self, points: List[List[int]]) -> int:
285+
length = len(points)
286+
if length == 0:
287+
return 0
288+
res = 1
289+
# 排序
290+
points.sort(key=lambda x: x[0])
291+
292+
end = points[0][1]
293+
# 遍历
294+
for i in range(1, length):
295+
if points[i][0] > end:
296+
res += 1
297+
end = points[i][1]
298+
else:
299+
end = min(end, points[i][1])
300+
301+
return res
302+
```
303+
271304
## 621. 任务调度器
272305

273306
[原题链接](https://leetcode-cn.com/problems/task-scheduler/)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /