문제 링크
https://school.programmers.co.kr/learn/courses/30/lessons/42626
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
시간복잡도 Olog(n)
"""
최소 힙의 0 index 가 key 이상일때까지
heapq 는 정렬 되어있지 않고 그냥 최솟값을 찾는 알고리즘임
"""
import heapq
def solution(scoville, K):
heap = scoville
heapq.heapify(heap)
answer = 0
while heap[0] < K:
first = heapq.heappop(heap)
second = heapq.heappop(heap)
heapq.heappush(heap,first + (second*2))
if len(heap) < 2:
if heap[0] < K: return -1
print(heap)
answer+=1
return answer
'코딩테스트 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 가장 큰수 - 퀵정렬 활용 (0) | 2024.11.04 |
---|---|
[프로그래머스] 게임 맵 최단거리 - BFS (0) | 2024.10.26 |
[프로그래머스] 소수 찾기 - 에라토스테네스의 채, 재귀 (0) | 2024.10.21 |
[프로그래머스] 표편집 - 스택, 인덱스 리스트, 링크드 리스트 (1) | 2024.10.16 |
[프로그래머스] 괄호 회전하기 - 스택,큐 (0) | 2024.10.12 |