Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 | 30 |
Tags
- 포천 관광지
- 현백 디저트
- 산정호수 한화리조트
- 글로우펏
- 을왕리데이트
- 바다앞꼬막집
- 청키파이
- 영종도맛집
- 영등포 가볼만한 곳
- 서빙로봇
- 룸식당
- 마살라 커리
- 송도 오마카세
- m1000RR
- 가성비 오마카세
- 서울로인
- 정글만리
- 서울 부모님과 식사
- 포천 가볼만한 곳
- 조정래
- 책
- 차덕분
- 중국
- 영종도디저트
- 인천맛집
- 구읍뱃터디저트
- 남양주 여행지
- 서울역 상견례
- 구읍뱃터맛집
- 이색데이트
Archives
- Today
- Total
ISTP의 간단명료 블로그
[프로그래머스] 더 맵게 / python 힙 자료구조 모듈 heapq 본문
문제

풀이
from heapq import heapify, heappop, heappush
def solution(scoville, K):
heapify(scoville)
answer=0
new_s=0
while scoville[0]<K and len(scoville)>1:
new_s=heappop(scoville)
new_s+=heappop(scoville)*2
heappush(scoville, new_s)
answer+=1
if scoville[0]<K:
return -1
return answer
다른사람 풀이
from heapq import heapify, heappush, heappop
def solution(scoville, K):
heapify(scoville)
for i in range(1000000):
try:
heappush(scoville, heappop(scoville)+(heappop(scoville)*2))
if scoville[0] >= K: return i+1
except:
return -1
배운점 : heapq
- 모듈 import
from heapq
- Heap 특징
- 정렬된 이진트리(binary tree) 👉 시간복잡도 O(log n)
- 리스트를 힙으로 변환 👉 heapify(리스트) / 원본리스트가 변경됨
- 원소 추가 👉 heappush(리스트, 원소)
- 원소 삭제 👉 heappop(리스트) : 가장 작은 값이 삭제됨
'풀다' 카테고리의 다른 글
| [프로그래머스] 날짜 비교하기 반례 테스트케이스 / 배열 부등호 (0) | 2023.06.29 |
|---|---|
| [프로그래머스] 디스크 컨트롤러 테스트 케이스 / deque (0) | 2023.06.23 |
| [프로그래머스] 프로세스 / 스택, 큐 / any, all (0) | 2023.06.18 |
| [프로그래머스] 기능개발 / 조건이 자연수인 경우 반영하기! (0) | 2023.06.13 |
| [프로그래머스] 완주하지 못한 선수 / 해시, Counter (0) | 2023.06.13 |