๐ 452. Minimum Number of Arrows to Burst Balloons
- 452.ย Minimum Number of Arrows to Burst Balloons
Minimum Number of Arrows to Burst Balloons - LeetCode
Input: points = [[10,16],[2,8],[1,6],[7,12]]
Output: 2
Explanation: The balloons can be burst by 2 arrows:
- Shoot an arrow at x = 6, bursting the balloons [2,8] and [1,6].
- Shoot an arrow at x = 11, bursting the balloons [10,16] and [7,12].
| 0 | 0 | 0 | 0 | 0 | 0 | ||
|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 0 | 0 |
points[1]๊ณผ poinst[2]๋ ์ด๋ฐ ์์ผ๋ก ํ์ ์ด ๋ฐฐ์น๋์ด ์๊ณ
ํ์ด์ y์ถ์ผ๋ก ๋ฌดํํ ๋ ์๊ฐ
๊ฐ์ฅ ์ ์ ํ์ด๋ก ํ์ ์ ํฐํธ๋ ค๋ผ
ํน์ง : X end โค X start ๊ฐ ํญ์ ์ฐธ
๊ฒฐ๊ตญ ํ์ ์ ํฐํธ๋ ค์ผ ํ๊ณ X end ๊ธฐ์ค์ผ๋ก ํฐํธ๋ ค ๋๊ฐ๋ฉด ๋จ ์๋ํ๋ฉด ์์ ์๋ผ๋ ํ์ ์ ํญ์ ํฐํธ๋ ค์ผ ํ๊ธฐ ๋๋ฌธ์
๋จผ์ X end ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ๋ค.
chat GPT ์๊ฒ[[x1, y1], [x2, y2]] ์ผ๋ y1, y2๋ฅผ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ๋ ค๋ฉด ์ด๋ป๊ฒ ํด์ผํ๋ ๋ฌผ์๋๋
lst = [[10,16],[2,8],[1,6],[7,12]]
sorted_lst = sorted(lst, key=lambda x: x[1])
print(sorted_lst)
์ด๋ ๊ฒ ํ๋ฉด ๋๋ค๊ณ ํ๋ค.
key๋ฅผ ์ด๋ค ์์๋ก ์ธ ์ ์๋์ง ์๋ ค์ฃผ๋ฉด ๋จ
from typing import List
class Solution:
def findMinArrowShots(self, points: List[List[int]]) -> int:
if not points:
return 0
points = sorted(points, key=lambda x: x[1])
count = 1
end = points[0][1]
for i in range(1, len(points)):
if points[i][0] > end:
count += 1
end = points[i][1]
return count
๋ต์ ์ด์ ๊ฐ๋ค. ๋งค์ฐ ์ฌํํ๋ค.
์ค์ํ๊ฒ์ ์ ์ผ ์์ ์๋ ํ์ ์ ์ธ์ ์ ์ผ ๋ง์ง๋ง์ ํฐํธ๋ฆด ์ ์์๊น๋ค.
โ ๋ โ