๋‚ด ์ธ์ƒ์—์„œ ๋ฏฟ์„ ๊ฑด ์˜ค์ง ๋‚˜ ์ž์‹ ๋ฟ!

The only one you can truly trust is yourself.

๊ฒŒ์ž„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ/Python ํ”„๋กœ๊ทธ๋ž˜๋ฐ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์•ผ๊ทผ์ง€์ˆ˜

๐ŸŽฎinspirer9 2023. 2. 25. 23:42
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ์„ค๋ช…

ํšŒ์‚ฌ์› Demi๋Š” ๊ฐ€๋”์€ ์•ผ๊ทผ์„ ํ•˜๋Š”๋ฐ์š”, ์•ผ๊ทผ์„ ํ•˜๋ฉด ์•ผ๊ทผ ํ”ผ๋กœ๋„๊ฐ€ ์Œ“์ž…๋‹ˆ๋‹ค. ์•ผ๊ทผ ํ”ผ๋กœ๋„๋Š” ์•ผ๊ทผ์„ ์‹œ์ž‘ํ•œ ์‹œ์ ์—์„œ ๋‚จ์€ ์ผ์˜ ์ž‘์—…๋Ÿ‰์„ ์ œ๊ณฑํ•˜์—ฌ ๋”ํ•œ ๊ฐ’์ž…๋‹ˆ๋‹ค. Demi๋Š” N์‹œ๊ฐ„ ๋™์•ˆ ์•ผ๊ทผ ํ”ผ๋กœ๋„๋ฅผ ์ตœ์†Œํ™”ํ•˜๋„๋ก ์ผํ•  ๊ฒ๋‹ˆ๋‹ค.Demi๊ฐ€ 1์‹œ๊ฐ„ ๋™์•ˆ ์ž‘์—…๋Ÿ‰ 1๋งŒํผ์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•  ๋•Œ, ํ‡ด๊ทผ๊นŒ์ง€ ๋‚จ์€ N ์‹œ๊ฐ„๊ณผ ๊ฐ ์ผ์— ๋Œ€ํ•œ ์ž‘์—…๋Ÿ‰ works์— ๋Œ€ํ•ด ์•ผ๊ทผ ํ”ผ๋กœ๋„๋ฅผ ์ตœ์†Œํ™”ํ•œ ๊ฐ’์„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜ solution์„ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ ์‚ฌํ•ญ

  • works๋Š” ๊ธธ์ด 1 ์ด์ƒ, 20,000 ์ดํ•˜์ธ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค.
  • works์˜ ์›์†Œ๋Š” 50000 ์ดํ•˜์ธ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.
  • n์€ 1,000,000 ์ดํ•˜์ธ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

works n result
[4, 3, 3] 4 12
[2, 1, 2] 1 6
[1,1] 3 0

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์ž…์ถœ๋ ฅ ์˜ˆ #1
n=4 ์ผ ๋•Œ, ๋‚จ์€ ์ผ์˜ ์ž‘์—…๋Ÿ‰์ด [4, 3, 3] ์ด๋ผ๋ฉด ์•ผ๊ทผ ์ง€์ˆ˜๋ฅผ ์ตœ์†Œํ™”ํ•˜๊ธฐ ์œ„ํ•ด 4์‹œ๊ฐ„๋™์•ˆ ์ผ์„ ํ•œ ๊ฒฐ๊ณผ๋Š” [2, 2, 2]์ž…๋‹ˆ๋‹ค. ์ด ๋•Œ ์•ผ๊ทผ ์ง€์ˆ˜๋Š” 22 + 22 + 22 = 12 ์ž…๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #2
n=1์ผ ๋•Œ, ๋‚จ์€ ์ผ์˜ ์ž‘์—…๋Ÿ‰์ด [2,1,2]๋ผ๋ฉด ์•ผ๊ทผ ์ง€์ˆ˜๋ฅผ ์ตœ์†Œํ™”ํ•˜๊ธฐ ์œ„ํ•ด 1์‹œ๊ฐ„๋™์•ˆ ์ผ์„ ํ•œ ๊ฒฐ๊ณผ๋Š” [1,1,2]์ž…๋‹ˆ๋‹ค. ์•ผ๊ทผ์ง€์ˆ˜๋Š” 12 + 12 + 22 = 6์ž…๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #3

๋‚จ์€ ์ž‘์—…๋Ÿ‰์ด ์—†์œผ๋ฏ€๋กœ ํ”ผ๋กœ๋„๋Š” 0์ž…๋‹ˆ๋‹ค.

ํ’€์ด

  • ์ด๊ฑฐ ์ฒ˜์Œ์— ใ…‹ใ…‹ใ…‹ ํ‰๊ท ์ด๋‚˜ ํŽธ์ฐจ๋กœ ํ•œ๋ฐฉ์— ๊ณ„์‚ฐ๋ ๊นŒ ์‹ถ์–ด์„œ ์ง€์ง€๊ณ  ๋ณต๋‹ค๊ฐ€ ์•ˆ๋˜์„œ
  • ํž™ ์จ์„œ 1์”ฉ ๋นผ๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ ํšจ์œจ์„ฑ ๊ฒ€์‚ฌ ์žˆ๊ธธ๋ž˜ ๋ญ‰์ณ์„œ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ฐ”๊ฟจ๋Š”๋ฐ ์ž๊พธ ์˜ค๋ฅ˜๊ฐ€ ๋‚˜...
  • ์™œ์ง€? ํ•˜๋ฉด์„œ 3์‹œ๊ฐ„ ๋‚ ๋ ธ๋Š”๋ฐ ๋ฆฌ์ŠคํŠธ๋ฅผ heapq๋กœ ๋ฐ”๊ฟ”๋ฒ„๋ฆฌ๊ณ  ๊ณ„์† ์ง€์ง€๊ณ  ๋ณถ์€ ๊ฑฐ...
  • ๊ทผ๋ฐ heapq๋กœ ๋ฐ”๊พธ๋ฉด ๊ฐ’์„ ๋„ฃ์„ ๋•Œ ๋งˆ๋‹ค ์ž๋™ ์ •๋ ฌํ•ด์คŒ. ๊ทธ๋ž˜์„œ ๊ฐ’์ด ๋ฐ”๋€Œ์–ด์„œ ๊ณ„์† ์‹คํŒจ๊ฐ€ ๋‚ฌ๋‹ค.
  • ๊ฒฐ๊ตญ heapify๋ฅผ ์ง€์šฐ๊ณ , ๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ค์–ด์„œ ํ’€์—ˆ๋‹ค.
  • ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด๊ฒƒ ์ €๊ฒƒ ์‹œ๋„ํ•˜๋ฉด์„œ ๊ผฌ์ด๋Š” ์‹ค์ˆ˜๋“ค์ด ์ ์  ๋งŽ์•„์ง„๋‹ค.
  • ์ฒ˜์Œ์— ๋”ฑ ์ •ํ•˜๋ฉด ๊ทธ๊ฑธ๋กœ๋งŒ ํ’€๊ณ , ์•ˆ๋  ๋•Œ๋Š” ๋‹ค ์ง€์šฐ๊ณ  ๋‹ค์‹œ ์งœ์•ผ ํ•˜๋Š”๋ฐ ์ฝ”๋“œ ์žฌํ™œ์šฉํ•˜๋ ค๋‹ค ๋ณด๋‹ˆ ์‰ฝ๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ๋„ ์ž๊พธ ํ‹€๋ฆฌ๋Š” ๊ฒƒ ๊ฐ™๋‹ค.
import heapq # ํ•„์š”์—†๋Š” ๋‚ด์šฉใ…‹ใ…‹ใ…‹

def solution(n, works):
    works.sort() # heapify(works)
    count_of_big, mods, answer = 1, 0, 0
    
    while n > 0:
        while len(works) > 1 and works[-1] == works[-2]: #ํฐ ์ˆซ์ž ๋‘ ๊ฐœ๊ฐ€ ๊ฐ™์€ ๊ฐ’์ด๋ฉด ํ•ฉ์ฒด
            count_of_big += 1
            works.pop(-1)
        if len(works) == 1: break # ๋ฐฐ์—ด์— ๊ฐ’์ด ํ•œ๊ฐœ ๋ฟ์ผ ๊ฒฝ์šฐ
        changes = min(n // count_of_big, works[-1] - works[-2])
        if changes == 0: break
        works[-1] -= changes
        n -= changes * count_of_big
        
    for i in range(len(works)-1):
        answer += works[i]**2
        
    if n == 0:
        answer += count_of_big * works[-1]**2
    else: # ๋‚จ์€ ์‹œ๊ฐ„์ด ์žˆ์œผ๋ฉด
        changes = n // count_of_big
        mods = n % count_of_big
        if mods == 0:
            works[-1] = max(0,works[-1] - changes)
            answer += count_of_big * works[-1] ** 2
        else:
            works[-1] = max(0,works[-1] - changes)
            answer += (count_of_big - mods) * works[-1] ** 2
            answer += (mods) * max(0,works[-1]-1) ** 2

    return answer
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.4MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.56ms, 10.3MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.65ms, 10.2MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.4MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.1MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํšจ์œจ์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (1.10ms, 10.2MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.98ms, 10.3MB)
  • ์ฝ”๋“œ๋Š” ์งง์ง€๋งŒ ์†๋„๊ฐ€ ๋‚˜์ง€ ์•Š๋Š” ๋ฐฉ์‹...
def solution(n, works):
    if n>=sum(works):
        return 0;

    while n > 0:
        works[works.index(max(works))] -= 1
        n -= 1

    answer = sum([w ** 2 for w in works])

    return answer
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.02ms, 10MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.4MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.30ms, 10.1MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.94ms, 10.3MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.1MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (0.00ms, 10.2MB)
ํšจ์œจ์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
ํ…Œ์ŠคํŠธ 2 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
  • ์ด๊ฒŒ ์›๋ž˜ ํ…Œ์ŠคํŠธ ๋ชฉ์ ์— ๋งž๋Š” ์ฝ”๋“œ, ์ตœ๋Œ€ํž™ ์‚ฌ์šฉ
from heapq import heapify, heappush, heappop
def solution(n, works):
    heapify(works := [-i for i in works])
    for i in range(min(n, abs(sum(works)))):
        heappush(works, heappop(works)+1)
    return sum([i*i for i in works])
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.2MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.1MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.07ms, 10.3MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.2MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.3MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.13ms, 10.2MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.19ms, 10.3MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.1MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํšจ์œจ์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (305.75ms, 10.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (319.33ms, 10.2MB)
  • ํ•˜์ง€๋งŒ ๋‚œ ๋น ๋ฆ„์„ ์ถ”๊ตฌํ•œ๋‹ค๊ทœ... ใ…ก.ใ…ก;
  • ์‚ฝ์งˆ ํ–ˆ์ง€๋งŒ ๋งŒ์กฑ.
728x90
๋ฐ˜์‘ํ˜•