|
์ฝ๋ ์์ฑํ๊ธฐ
import heapq
def solution(scoville, K):
answer = 0
heapq.heapify(scoville)
while scoville:
now = heapq.heappop(scoville)
if now < K:
if scoville:
second = heapq.heappop(scoville)
heapq.heappush(scoville, now + 2*second)
answer += 1
else:
break
else:
return answer
return -1
Python
๋ณต์ฌ
|
์ฝ๋ ์ค๋ช
ํ๊ธฐ
heap์ ์ด์ฉํ์ฌ ๊ฐ์ฅ ์์ ์์๊ฐ K๋ณด๋ค ์์ผ๋ฉด ๋๋ฒ์งธ ์์ ์์ 2๋ฐฐ์ ๋ํ์ฌ ๋ค์ pushํ๊ณ , K๋ณด๋ค ํฌ๋ฉด ์ด๋๊น์ง ์์ ์๋ฅผ ๋ฐํํ๋ค.
Scoville์ด ๋๋๊ฑฐ๋ ๋๋ฒ์งธ ์์ ์๊ฐ ์์ผ๋ฉด -1์ ๋ฐํํ๋ค.