https://school.programmers.co.kr/learn/courses/30/lessons/42626
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
전체 코드
import heapq
def solution(scoville, K):
heapq.heapify(scoville)
count = 0
while True:
curr = heapq.heappop(scoville)
if curr < K:
try:
count += 1
heapq.heappush(scoville, curr + (heapq.heappop(scoville) * 2))
except:
return -1
else: break
return count
헷갈렸던 부분
heapq.heapify(x)를 하면 heap이 반환되는 게 아니라 리스트x가 heap 구조로 바뀐다.
또, heapify는 O(n)이기 때문에 신중히 사용하자.
'Coding Test > Python' 카테고리의 다른 글
[LeetCode] 290. Word Pattern (easy, python) (0) | 2024.01.15 |
---|---|
[LeetCode] 383. Ransom Note (easy, python) (0) | 2024.01.15 |
[프로그래머스] 프로세스 (level2, python) (0) | 2024.01.11 |
[프로그래머스] 올바른 괄호 (level2, python) (0) | 2024.01.11 |
[프로그래머스] 기능개발 (level2, python) (1) | 2024.01.11 |