[ํ๋ก๊ทธ๋๋จธ์ค-Lv2] ๋ ๋งต๊ฒ / Python
๐ Problem Solving/Programmers
import sys
import heapq
input = sys.stdin.readline
scoville = list(map(int, input().split()))
k = int(input())
def solution(scoville, k):
scoville.sort()
answer = 0
while len(scoville) > 0:
if scoville[0] >= k:
return answer
temp1 = heapq.heappop(scoville)
if scoville:
heapq.heappush(scoville, temp1 + (heapq.heappop(scoville) * 2))
answer += 1
return -1
print(solution(scoville, k))
ํด์ค
heap์ ์ด์ฉํด ํ ์ ์์๋ ๋ฌธ์ ๋ค.
๋ณ๋ค๋ฅธ ์ ๋ ฌ์์ด ์ ์ผ ์์ ๊ฐ์ ์ถ์ถํ ์ ์์ผ๋ฏ๋ก, ํจ์จ์ฑํ ์คํธ์์๋ ๋ฌธ์ ๊ฐ ์๋ค.
'๐ Problem Solving > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค-Lv2] ์ ํ๋ฒํธ ๋ชฉ๋ก / Python (0) | 2021.06.02 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค-Lv2] ํ๊ฒ ๋๋ฒ / Python (0) | 2021.06.02 |
[ํ๋ก๊ทธ๋๋จธ์ค-Lv2] ๊ธฐ๋ฅ๊ฐ๋ฐ (0) | 2021.06.02 |
[ํ๋ก๊ทธ๋๋จธ์ค-Lv1] [1์ฐจ] ๋น๋ฐ์ง๋ / Python (0) | 2021.06.02 |
[ํ๋ก๊ทธ๋๋จธ์ค-Lv1] ๋ ๊ฐ ๋ฝ์์ ๋ํ๊ธฐ / Python (0) | 2021.06.02 |