프로그래밍/코딩테스트 공부
[프로그래머스] 힙 문제 - 더 맵게
진박사.
2021. 9. 10. 08:37
import heapq
def solution(scoville, K):
answer = 0
heap = []
for s in scoville:
heapq.heappush(heap, s)
while True:
now_scovile = heapq.heappop(heap)
if now_scovile >= K: # 최소값이 K 이상이면
break # 끝
if not heap: #heap이 비었다면
return -1 #K를 만들 수 없었음
else: #힙이 1 이상 남았으면 섞을 수 있음
heapq.heappush(heap, now_scovile + heapq.heappop(heap)*2) #섞은 음식의 스코빌 지수를 계산해 넣기
answer += 1 #섞은 횟수 증가
return answer
17분... 휴 할만 하군
중간에 heap에 1개만 남아있을 때 더이상 못 섞어주는데 1번째와 0번째 섞으려고 해서 런타임 오류 뜬거 빼고는 빠르게 푼듯.