반응형
Notice
Recent Posts
Recent Comments
Link
동캄의 코딩도장
프로그래머스 level2 [더 맵게] 파이썬 본문
반응형
https://programmers.co.kr/learn/courses/30/lessons/42626
코딩테스트 연습 - 더 맵게
매운 것을 좋아하는 Leo는 모든 음식의 스코빌 지수를 K 이상으로 만들고 싶습니다. 모든 음식의 스코빌 지수를 K 이상으로 만들기 위해 Leo는 스코빌 지수가 가장 낮은 두 개의 음식을 아래와 같
programmers.co.kr
#프로그래머스 더 맵게
def solution(scoville, K):
import heapq
answer = 0
heapq.heapify(scoville)
leng=len(scoville)
while scoville[0]<K and leng>1:
x1=heapq.heappop(scoville)
x2=heapq.heappop(scoville)
new= x1+x2*2
heapq.heappush(scoville,new)
leng-=1
answer+=1
if scoville[0]>=K:
return answer
else:
return -1
파이썬 heapq 내장함수를 이용하여 해결하였다.
*알게된사실: (걸리는 시간 pop()>heappop()이다.)
반응형
'코테 > 프로그래머스' 카테고리의 다른 글
프로그래머스 level2 [튜플] 파이썬 (0) | 2021.12.17 |
---|---|
프로그래머스 level2 [전화번호 목록] 파이썬 (0) | 2021.12.11 |
프로그래머스 level2 [오픈채팅방] 파이썬 (0) | 2021.12.10 |
프로그래머스 level2 [위장] 파이썬 (0) | 2021.12.10 |
프로그래머스 level2 [카펫] 파이썬 (0) | 2021.12.10 |