|
์ฝ๋ ์์ฑํ๊ธฐ
import heapq
def solution(scoville, K):
heapq.heapify(scoville)
answer=0
while scoville[0]<K :
if len(scoville)==1 :
return -1
a=heapq.heappop(scoville)
b=heapq.heappop(scoville)
heapq.heappush(scoville, a+b*2)
answer+=1
return answer
Python
๋ณต์ฌ
|
์ฝ๋ ์ค๋ช
ํ๊ธฐ
1.
scoville ๋ฆฌ์คํธ๋ฅผ ํํ๋ก ๋ณ๊ฒฝ
2.
์ค์ฝ๋น์ง์๊ฐ ์ ์ผ ์์ ๊ฐ์ด K๋ณด๋ค ์์ ๋ ๋ฐ๋ณตํ๋ค.
a.
scoville ๋ฆฌ์คํธ์ ๊ธธ์ด๊ฐ 1์ด๋ฉด ๋ ์ด์ ์งํํ ์ ์์ผ๋ฏ๋ก -1์ ๋ฐํํ๋ค.
b.
์ ์ผ ์์ ๊ฐ๊ณผ ๋๋ฒ์งธ๋ก ์์ ๊ฐ์ ํํ์์ ์ญ์ ํ๊ณ ์ ์ผ ์์ ๊ฐ+ ๋๋ฒ์งธ๋ก ์์ ๊ฐ*2๋ฅผ ๊ณ์ฐํ ๊ฐ์ ํํ์ ๋ฃ๋๋ค.
c.
answer๋ฅผ 1 ์ฆ๊ฐ์ํจ๋ค.