일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- recoil
- 이분 검색
- dfs
- 결정 알고리즘
- React
- useAppDispatch
- 반공변성
- TS
- tailwind
- map
- 무한 스크롤
- app router
- 투포인터
- RTK Query
- webpack
- 타입 좁히기
- ESlint
- CI/CD
- CORS
- autosize
- Cypress
- 리터럴 타입
- SSR
- 공변성
- 인터섹션
- 호이스팅
- Jest
- 태그된 유니온
- Promise
- async/await
Archives
- Today
- Total
짧은코딩
프로그래머스) 더 맵게 본문
반응형
https://programmers.co.kr/learn/courses/30/lessons/42626
내 풀이(질문 봄)
import heapq
def solution(scoville, K):
answer = 0
scoville.sort()
sum = 0
while scoville[0] < K:
if len(scoville) <= 1:
answer = -1
break
a = heapq.heappop(scoville)
b = heapq.heappop(scoville)
sum = a + (b*2)
heapq.heappush(scoville, sum)
answer += 1
return answer
거의 다 맞췄지만 처음에 sort()로 정렬을 해줘야하는 것을 빼 먹었다. sort()를 하면 heapfiy를 안해줘도 된다. 이렇게해서 scoville에 있는 모든 원소가 K보다 커질 때 까지 반복한다. 만약 scoville가 1개가 남았으면 더 이상 진행할 수 없다. 그래서 answer에 -1을 넣고 멈춘다.
반응형
'코딩 테스트(Python) > 백준, 프로그래머스' 카테고리의 다른 글
2178 미로 탐색 (1) | 2022.06.04 |
---|---|
10815 숫자 카드 (1) | 2022.06.02 |
프로그래머스) 기능개발 (0) | 2022.05.01 |
1766 문제집 (0) | 2022.04.15 |
7662 이중 우선순위 큐 (0) | 2022.04.08 |
Comments