Skip to main content

๐Ÿš€ 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].
000000
0000000

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

๋‹ต์€ ์ด์™€ ๊ฐ™๋‹ค. ๋งค์šฐ ์‹ฌํ”Œํ•˜๋‹ค.

์ค‘์š”ํ•œ๊ฒƒ์€ ์ œ์ผ ์•ž์— ์žˆ๋Š” ํ’์„ ์€ ์–ธ์ œ ์ œ์ผ ๋งˆ์ง€๋ง‰์— ํ„ฐํŠธ๋ฆด ์ˆ˜ ์žˆ์„๊นŒ๋‹ค.

โ† ๋ โ†’