https://school.programmers.co.kr/learn/courses/30/lessons/42626
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
#스코빌지수가 가장 낮은 거 + 두 번째로 낮은 거*2 합치기. 모든 음식의 스코빌지수가 k가 넘을 때까지
heapq 쓰면 좋은 점:
더해서 나온 값이 정렬된 리스트에서 제자리를 찾아서 들어가야 계속해서 스코빌지수가 제일 낮은 음식을 heappop으로 구할 수 있다.
import heapq
def solution(scoville, k):
heapq.heapify(scoville)
count=0
while scoville[0]<k:
if len(scoville)==1:
return -1
a=heapq.heappop(scoville)
b=heapq.heappop(scoville)
heapq.heappush(scoville, a+(b*2))
count+=1
return count
'알고리즘 PS > data structure' 카테고리의 다른 글
파이썬에서 해시맵 구현은 딕셔너리 (0) | 2024.07.26 |
---|---|
921. Minimum Add to Make Parentheses Valid (0) | 2024.06.25 |
요세푸스, dedque.rotate( )로 리스트 회전하기 (0) | 2024.06.23 |
1512. Number of Good Pairs 파이썬 defualtdict 이용해서 해시로 풀기 (0) | 2024.06.15 |
그래프 구현 기본 (0) | 2024.05.30 |