[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค-Lv2] ๋” ๋งต๊ฒŒ / Python

๐Ÿ“š Problem Solving/Programmers

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๋” ๋งต๊ฒŒ

๋งค์šด ๊ฒƒ์„ ์ข‹์•„ํ•˜๋Š” Leo๋Š” ๋ชจ๋“  ์Œ์‹์˜ ์Šค์ฝ”๋นŒ ์ง€์ˆ˜๋ฅผ K ์ด์ƒ์œผ๋กœ ๋งŒ๋“ค๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค. ๋ชจ๋“  ์Œ์‹์˜ ์Šค์ฝ”๋นŒ ์ง€์ˆ˜๋ฅผ K ์ด์ƒ์œผ๋กœ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด Leo๋Š” ์Šค์ฝ”๋นŒ ์ง€์ˆ˜๊ฐ€ ๊ฐ€์žฅ ๋‚ฎ์€ ๋‘ ๊ฐœ์˜ ์Œ์‹์„ ์•„๋ž˜์™€ ๊ฐ™

programmers.co.kr

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์„ ์ด์šฉํ•ด ํ’€ ์ˆ˜ ์žˆ์—ˆ๋˜ ๋ฌธ์ œ๋‹ค. 

๋ณ„๋‹ค๋ฅธ ์ •๋ ฌ์—†์ด ์ œ์ผ ์ž‘์€ ๊ฐ’์„ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ํšจ์œจ์„ฑํ…Œ์ŠคํŠธ์—์„œ๋„ ๋ฌธ์ œ๊ฐ€ ์—†๋‹ค.