๋ฌธ์
https://school.programmers.co.kr/learn/courses/30/lessons/142085
ํ๋ก๊ทธ๋๋จธ์ค
์ฝ๋ ์ค์ฌ์ ๊ฐ๋ฐ์ ์ฑ์ฉ. ์คํ ๊ธฐ๋ฐ์ ํฌ์ง์ ๋งค์นญ. ํ๋ก๊ทธ๋๋จธ์ค์ ๊ฐ๋ฐ์ ๋ง์ถคํ ํ๋กํ์ ๋ฑ๋กํ๊ณ , ๋์ ๊ธฐ์ ๊ถํฉ์ด ์ ๋ง๋ ๊ธฐ์ ๋ค์ ๋งค์นญ ๋ฐ์ผ์ธ์.
programmers.co.kr
ํ์ด
ํ์ ์ฌ์ฉํ๋ ๋ฌธ์ ๋ค.
import heapq
def solution(n: int, k: int, enemy: list) -> int:
"""
round: ํต๊ณผํ ๋ผ์ด๋์ ์
enemies: ํ์ฌ๊น์ง ์๋ํ ์ ์ ์
"""
round = enemies = 0
heap = []
for e in enemy:
heapq.heappush(heap, -e)
enemies += e
if enemies > n:
if not k:
break
k -= 1
enemies += heapq.heappop(heap)
round += 1
return round
๋ค๋ฅธ ํ์ด์ ๋ณ ๋ค๋ฅผ๋ฐ ์๊ธฐ์ ์ค๋ช
์ ์๋ตํ๋ค.
ํ์ด ๊ณผ์ :
1. heapq ๋ชจ๋์ importํ๋ค.
2. ํต๊ณผํ ๋ผ์ด๋์ ์๋ฅผ ๋ด์ round, ํ์ฌ๊น์ง ๋ณ์ฌ๋ค๋ก ์๋ํ ์ ์ ์ enemies๋ฅผ 0์ผ๋ก ์ด๊ธฐํํ๋ค.
(๊ทผ๋ฐ ์ฐ๋ฉด์ ์์์ฐจ๋ฆฐ๊ฑด๋ฐ ๋ณ์๋ช ์ผ๋ก round๋ ์ ์ ํ์ง ์๋ค... ๊ธฐ์กด์ ์๋ ํจ์๋ช ๊ณผ ๊ฒน์นจ)
3. ๊ฐ ๋ผ์ด๋๋ณ ์ ์ ์๊ฐ ๋ด๊ฒจ์๋ enemy๋ฅผ ์ํํ๋ฉฐ heap์ pushํ๋ค.
3.1. ํ์ด์ฌ์ heapq์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ต์ํ์ด๊ธฐ ๋๋ฌธ์ -๋ฅผ ๋ถ์ฌ ์์๋ก ์ง์ด๋ฃ๋๋ค.
3.2. ์ ์ ์ e๋ฅผ enemies์ ๋ํ๋ค.
4. ๋ง์ฝ enemies๊ฐ n๋ณด๋ค ์ปค์ง ๊ฒฝ์ฐ,
4.1. ๋ฌด์ ๊ถ k๊ฐ ์กด์ฌํ์ง ์๋๋ค๋ฉด break
4.2. ์กด์ฌํ๋ค๋ฉด k๋ฅผ 1 ์ฐจ๊ฐํ๊ณ heap์์ ๊ฐ์ฅ ์์ ๊ฐ(-๋ฅผ ๋ถ์์ผ๋ฏ๋ก ์ค์ง์ ์ผ๋ก๋ ๊ฐ์ฅ ํฐ ์ ์ ์๊ฐ ๋๋ค)์ popํ๋ค, enemies์ ๋ํด์ค๋ค. ์ฆ, ์ฌํ๊น์ง ์๋ํ ์ ์ ์ ์ค ๊ฐ์ฅ ๋ง์ ์ ์ ์๋ฅผ popํ๋ค enemies์์ ์ฐจ๊ฐํ๋ค. (๋ฌด์ ๊ถ์ผ๋ก ์๋ํ์ผ๋ฏ๋ก ์ฌ์ค์ ์๋ํ์ง ์์ ์
์ด๋ค.)
5. ํ๋ฒ ์ํํ ๋๋ง๋ค round๋ฅผ 1 ์ฆ๊ฐ์ํจ๋ค.
6. round ๋ฐํ.
ํ(heap)์ด๋?
: ์ต๋๊ฐ/์ต์๊ฐ์ ๋น ๋ฅด๊ฒ ์ฐพ์ ์ ์๋๋ก ๋ง๋ค์ด์ง ์์ ์ด์งํธ๋ฆฌ ํ์์ ์๋ฃ๊ตฌ์กฐ.
์ต๋ํ(Max Heap): ๋ถ๋ชจ ๋ ธ๋์ ํค ๊ฐ์ด ์์ ๋ ธ๋์ ํค ๊ฐ๋ณด๋ค ํญ์ ํฌ๊ฑฐ๋ ๊ฐ์
์ต์ํ(Min Heap): ๋ถ๋ชจ ๋ ธ๋์ ํค ๊ฐ์ด ์์ ๋ ธ๋์ ํค ๊ฐ๋ณด๋ค ํญ์ ์๊ฑฐ๋ ๊ฐ์
ํน์ง
- ์์ ์ด์ง ํธ๋ฆฌ ๊ตฌ์กฐ
- ๋ฃจํธ ๋ ธ๋๋ ํญ์ ์ต์๊ฐ(์ต์ํ) ๋๋ ์ต๋๊ฐ(์ต๋ํ)์ ๊ฐ์ง
- ๋ฐฐ์ด์ ์ฌ์ฉํ์ฌ ๊ตฌํ. ๋ฐฐ์ด์ ์ฒซ๋ฒ์งธ ์์(์ธ๋ฑ์ค 0)์ ์ฌ์ฉํ์ง ์๊ฑฐ๋ ๋ฃจํธ ๋ ธ๋๋ก ์ฌ์ฉํจ
- ๋ถ๋ชจ ๋
ธ๋์ ์์ ๋
ธ๋์์ ๊ด๊ณ
- ๋ถ๋ชจ ๋ ธ๋์ ์ธ๋ฑ์ค๋ฅผ i๋ผ๊ณ ํ ๋, ์ผ์ชฝ ์์ ๋ ธ๋์ ์ธ๋ฑ์ค๋ 2i, ์ค๋ฅธ์ชฝ ์์ ๋ ธ๋์ ์ธ๋ฑ์ค๋ 2i+1
- ์์ ๋ ธ๋์ ์ธ๋ฑ์ค๋ฅผ j๋ผ๊ณ ํ ๋, ๋ถ๋ชจ ๋ ธ๋์ ์ธ๋ฑ์ค๋ j//2
Python์์์ ํ ์ฌ์ฉ
ํ์ด์ฌ์ heapq๋ชจ๋์ ์ฌ์ฉํ์ฌ ์ฝ๊ฒ ๊ตฌํํ ์ ์๋ค. ๊ธฐ๋ณธ์ ์ผ๋ก ์ต์ํ์ด๋ค.
์ฃผ์ ๋ฉ์๋
heapq.heappush(heap, item): item์ ํ์ ์ฝ์heapq.heappop(heap): ํ์์ ์ต์๊ฐ์ ์ ๊ฑฐํ๊ณ ๋ฐํheapq.heapify(x): ๋ฆฌ์คํธ x๋ฅผ ํ์ผ๋ก ๋ณํheapq.nlargest(n, iterable): iterable์์ ๊ฐ์ฅ ํฐ n๊ฐ์ ์์๋ฅผ ๋ฐํheapq.nsmallest(n, iterable): iterable์์ ๊ฐ์ฅ ์์ n๊ฐ์ ์์๋ฅผ ๋ฐํ
์ฌ์ฉ ์์
import heapq
# ๋น ํ ์์ฑ
heap = []
# ํ์ ์์ ์ฝ์
heapq.heappush(heap, 4)
heapq.heappush(heap, 1)
heapq.heappush(heap, 7)
heapq.heappush(heap, 3)
print(heap) # [1, 3, 7, 4]
# ํ์์ ์ต์๊ฐ ์ ๊ฑฐ ๋ฐ ๋ฐํ
min_value = heapq.heappop(heap)
print(min_value) # 1
print(heap) # [3, 4, 7]
# ๋ฆฌ์คํธ๋ฅผ ํ์ผ๋ก ๋ณํ
list1 = [5, 2, 9, 1, 7]
heapq.heapify(list1)
print(list1) # [1, 2, 9, 5, 7]
# ์ต๋๊ฐ ๋ฐ ์ต์๊ฐ ์ฐพ๊ธฐ
list2 = [4, 2, 9, 7, 5]
largest_three = heapq.nlargest(3, list2)
smallest_two = heapq.nsmallest(2, list2)
print(largest_three) # [9, 7, 5]
print(smallest_two) # [2, 4]
์ต๋ํ ๊ตฌํํ๊ธฐ
๋งํ๋ค์ํผ ๊ธฐ๋ณธ์ ์ผ๋ก ์ต์ํ์ผ๋ก ๋์ํ๊ธฐ ๋๋ฌธ์, ์ฝ๊ฐ์ ๋ณํ์ ๊ฑฐ์ณ์ผํ๋ค.
๋ฐ๋ก ํ์ ์์๋ฅผ ์ฝ์ ํ ๋ ์์๋ฅผ ๋ถ์ด๋๊ฒ์ด๋ค. ์ด๋ ๊ฒํ๋ฉด ๊ฐ์ฅ ์์ ์์๊ฐ ์ค์ง์ ์ผ๋ก๋ ๊ฐ์ฅ ํฐ ์์๊ฐ ๋๋ ์ .
(sort ์ฌ์ฉ ์ key์ ๊ธฐ์ค์ด ์ฌ๋ฌ๊ฐ์ผ๋. ์ค๋ฆ์ฐจ์/๋ด๋ฆผ์ฐจ์ ๋ ๋ค ๊ณ ๋ คํด์ผํ ๋๋ฅผ ์๊ฐํ์.)
import heapq
# ๋น ํ ์์ฑ
max_heap = []
# ์ต๋ ํ์ ์์ ์ฝ์
heapq.heappush(max_heap, -4)
heapq.heappush(max_heap, -1)
heapq.heappush(max_heap, -7)
heapq.heappush(max_heap, -3)
print(max_heap) # [-7, -3, -1, -4]
# ์ต๋ ํ์์ ์ต๋๊ฐ ์ ๊ฑฐ ๋ฐ ๋ฐํ
max_value = -heapq.heappop(max_heap)
print(max_value) # 7
print(max_heap) # [-4, -3, -1]