[Programmers] μ•Όκ·Ό μ§€μˆ˜

Updated:

μ•Όκ·Ό μ§€μˆ˜

μ•Όκ·Ό μ§€μˆ˜ λ₯Ό ν΄λ¦­ν•˜λ©΄ λ°”λ‘œ μ΄λ™ν•œλ‹€.

μ•Όκ·Ό μ§€μˆ˜λŠ” 야근을 ν•  λ•Œ 남은 μž‘μ—…λŸ‰μ˜ 제곱의 합이닀.

λ•Œλ¬Έμ— 야근을 ν•˜κΈ° μ „ κΉŒμ§€ μ΅œλŒ€ν•œ μž‘μ—…λŸ‰μ΄ λ§Žμ€ μž‘μ—…λ“€μ„ 쀄여 λ†“μœΌλ©΄ λœλ‹€.

즉, μ‹œκ°„μ΄ 갈 λ•Œ λ§ˆλ‹€ λ°°μ—΄μ˜ μ΅œλŒ“κ°’μ—μ„œ 1을 λΉΌμ£Όλ©΄ λœλ‹€.

μž‘μ—…μ„ ν•  λ•Œ λ§ˆλ‹€ μ΅œλŒ“κ°’μ„ μ°Ύμ•„μ•Ό ν•˜κΈ° λ•Œλ¬Έμ— max ν•¨μˆ˜λ₯Ό μ΄μš©ν•˜λ©΄ μ‹œκ°„μ΄ˆκ³Όκ°€ λ°œμƒν•œλ‹€.

이럴 λ•Œ μ΅œλŒ€νž™μ„ μ‚¬μš©ν•˜λ©΄ λœλ‹€.

ν•˜μ§€λ§Œ νŒŒμ΄μ¬μ€ μ΅œμ†Œνž™λ§Œμ„ μ œκ³΅ν•˜κΈ° λ•Œλ¬Έμ— μ›λž˜ 값에 -1 을 κ³±ν•΄ μ΅œλŒ€νž™μ„ κ΅¬ν˜„ν•œλ‹€.

μ œκ³±μ„ ν•΄μ„œ λ”ν•˜λΌλŠ” μ΄μœ κ°€ μ΅œλŒ€νž™μ„ κ΅¬ν˜„ν•˜λ©΄ -1 을 κ³±ν•΄μ•Ό ν•˜λ‹ˆ 힌트λ₯Ό μ€€ 것인가..


import heapq


def solution(n, works):
	result = 0

	if sum(works) <= n:
		return 0

	for i in range(len(works)):
		works[i] *= -1

	heapq.heapify(works)
	for i in range(n):
		work = heapq.heappop(works) * -1
		work -= 1
		heapq.heappush(works, work * -1)

	for work in works:
		result += (work ** 2)

	return result



Categories:

Updated:

Leave a comment