문제 풀이 코드
import heapq
def solution(scoville, K):
answer = 0
heapq.heapify(scoville) #1
while True:
answer += 1
heapq.heappush(scoville,heapq.heappop(scoville) + heapq.heappop(scoville) * 2) #2
if scoville[0] >= K: #3
return answer
elif len(scoville) <= 1:
return -1
#1
기존 scoville배열을 heap 자료구조로 변경시킴 (O(N))
#2
맨 앞 + (앞에서 두번째 X 2) 를 heap queue에 다시 집어넣음
#3
만약 가장 작은 인자가 K보다 크다면 섞은 횟수 반환
'코딩테스트' 카테고리의 다른 글
| [코딩테스트] 프로그래머스 월간 코드 챌린지 내적 with Python (0) | 2021.05.14 |
|---|---|
| [코딩테스트] 타겟 넘버 with Python (0) | 2021.05.13 |
| [코딩테스트] 전화번호 목록 with Python (0) | 2021.05.11 |
| [코딩테스트] 프로그래머스 프린터 with Python (0) | 2021.05.10 |
| [코딩테스트] 프로그래머스 다리를 지나는 트럭 with Python (0) | 2021.05.09 |