[Programmers] 더 맡게

Updated:

더 맡게

더 맡게 λ₯Ό ν΄λ¦­ν•˜λ©΄ λ°”λ‘œ μ΄λ™ν•œλ‹€.

λ¬Έμ œκ°€ νž™ μΉ΄ν…Œκ³ λ¦¬μ— μžˆμ–΄μ„œ νž™μœΌλ‘œ μ ‘κ·Όν•΄μ•Ό ν•˜λŠ” κ±Έ μ•Œμ•˜μ§€λ§Œ..

μ§„μ§œκ°€ 되렀면 문제λ₯Ό 보고 β€˜νž™μ„ 써야 금방 ν•˜κ² λ‹€ !’ λΌλŠ” 생각이 λ“€μ–΄μ•Ό ν•œλ‹€.

μ²˜μŒμ—” μ΅œμ†Œ νž™ λŒ€μ‹  μš°μ„ μˆœμœ„νλ₯Ό μ¨λ΄€λŠ”λ° 정확도 λ¬Έμ œλŠ” λ‹€ λ§žλŠ” 반면 νš¨μœ¨μ„±μ—μ„  μ „λΆ€ ν‹€λ Έλ‹€.

λ‹Ήμ—°ν•œ μ΄μ•ΌκΈ°κ² μ§€λ§Œ μ΅œμ†Œ νž™μ€ 루트 값을 λ½‘μ•„μ˜€λ©΄ λ˜μ§€λ§Œ μš°μ„ μˆœμœ„ νλŠ” 제일 μž‘μ€ 값을 μ°Ύμ•„μ•Ό ν•˜κΈ° λ•Œλ¬Έμ΄λ‹€ !


import heapq


def solution(scoville, k):
	answer = 0
	min_heap = list()

	for val in scoville:
		heapq.heappush(min_heap, val)

	while True:
		try:
			num1 = heapq.heappop(min_heap)
			num2 = heapq.heappop(min_heap)
			num3 = num1 + (num2 * 2)
			heapq.heappush(min_heap, num3)
			answer += 1

		except IndexError:
			answer = -1
			break

		if min_heap[0] >= k:
			break

	return answer

μ½”λ“œ κ·ΈλŒ€λ‘œ μ­‰ μ„€λͺ…해보면 scoville 을 톡해 min_heap 을 λ§Œλ“  λ’€

μ„žμ–΄μ„œ λ§Œλ“  μŠ€μ½”λΉŒ κ°’ 쀑 μ΅œμ†Ÿκ°’μ΄ K λ₯Ό λ„˜μœΌλ©΄ μ’…λ£Œν•œλ‹€.

ν•˜μ§€λ§Œ λκΉŒμ§€ μ„žμ—ˆμŒμ—λ„ λΆˆκ΅¬ν•˜κ³  μ΅œμ†Ÿκ°’μ΄ K λ₯Ό λ„˜μ§€ λͺ»ν•˜λ©΄ -1을 λ°˜ν™˜ ν•΄μ•Ό ν•œλ‹€.

이 κ²½μš°λŠ” λ§ˆμ§€λ§‰μ— num2 λ₯Ό pop ν•˜λŠ” κ³Όμ •μ—μ„œ IndexError κ°€ λ°œμƒ ν•΄ try, except 둜 μ²˜λ¦¬ν•΄μ£Όμ—ˆλ‹€.


Categories:

Updated:

Leave a comment