https://programmers.co.kr/learn/courses/30/lessons/42626
def solution(scoville, K):
answer = 0
scovile = sorted(scoville)
l = 0
while scoville[0] < K:
scoville[0] = scoville[0] + scoville[1] * 2
scoville.pop(1)
scoville.sort()
#print(scoville)
answer += 1
l += 1
if l >= len(scoville):
return -1
return answer
아! 이거 힙 쓰는 문제였지!!
import heapq
def solution(scoville, K):
answer = 0
heap = []
for item in scoville:
heapq.heappush(heap, item)
#print(heap)
while heap[0] < K:
heapq.heappush(heap, heapq.heappop(heap) + heapq.heappop(heap) * 2)
answer += 1
return answer
거의 다 됐다
import heapq
def solution(scoville, K):
answer = 0
heap = []
for item in scoville:
heapq.heappush(heap, item)
while heap[0] < K:
try:
heapq.heappush(heap, heapq.heappop(heap) + heapq.heappop(heap) * 2)
except:
return -1
answer += 1
return answer
나이스!