효율성에서 계속 걸렸는데 while문에서 min(scoville) 나 heapq.nsmallest(scoville, 1) 했던걸 scoville[0] 하니까 통과됐다!
import heapq
def solution(scoville, K):
answer = 0
scoville.sort()
if scoville[0]>=K:
return 0
heapq.heapify(scoville)
while scoville[0]<K:
if len(scoville)==1:
answer=-1
break
heapq.heappush(scoville,heapq.heappop(scoville)+(heapq.heappop(scoville)*2))
answer+=1
return answer
반응형
'Programming > Python' 카테고리의 다른 글
[Python3] Programmers - N으로 표현 (DP) (1) | 2024.03.21 |
---|---|
[Python3] 프로그래머스 입국 심사 - 이진탐색 (level 3) (0) | 2023.10.21 |
[Python3] 프로그래머스 H-Index - 정렬 (0) | 2023.10.21 |
[Python3] 프로그래머스 K번째 수 - 정렬 (0) | 2023.10.21 |
[Python3] 프로그래머스 여행 경로 - DFS (재귀, 예외처리) (1) | 2023.10.21 |