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

The only one you can truly trust is yourself.

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

[PCCP ๋ชจ์˜๊ณ ์‚ฌ#2] ์‹ ์ž…์‚ฌ์› ๊ต์œก (์šฐ์„ ์ˆœ์œ„ํ)

๐ŸŽฎinspirer9 2023. 3. 10. 09:00
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ์„ค๋ช…

์‚ฐ์—…์ŠคํŒŒ์ด ๋ฏผ์ˆ˜๋Š” AํšŒ์‚ฌ์— ์œ„์žฅ ์ทจ์—…ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ๋ชจ๋ฅด๋Š” ๋ฏผ์ˆ˜์˜ ์ƒ์‚ฌ๋Š” ์‹ ์ž…์‚ฌ์› ๊ต์œก ์ค‘ ์ผ๋ถ€๋ฅผ ๋ฏผ์ˆ˜์—๊ฒŒ ๋งก๊ฒผ์Šต๋‹ˆ๋‹ค. ๋ฏผ์ˆ˜๊ฐ€ ๋งก์€ ์ž„๋ฌด๋Š” ์‹ ์ž…์‚ฌ์› ์ค‘ 2๋ช…์„ ์„ ๋ฐœํ•˜๊ณ  ์„ ๋ฐœ๋œ 2๋ช…์ด ๊ฐ™์ด ๊ณต๋ถ€ํ•˜๊ฒŒ ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ชจ๋“  ์‹ ์ž…์‚ฌ์›๋“ค์˜ ๋Šฅ๋ ฅ์น˜๋Š” ์ •์ˆ˜๋กœ ํ‘œํ˜„๋˜์–ด ์žˆ๋Š”๋ฐ, 2๋ช…์˜ ์‹ ์ž…์‚ฌ์›์ด ๊ฐ™์ด ๊ณต๋ถ€ํ•˜๋ฉด ์„œ๋กœ์˜ ๋Šฅ๋ ฅ์„ ํก์ˆ˜ํ•˜์—ฌ ๋‘ ์‹ ์ž…์‚ฌ์›์˜ ๋Šฅ๋ ฅ์น˜๋Š” ๊ณต๋ถ€ ์ „ ๋‘ ์‚ฌ๋žŒ์˜ ๋Šฅ๋ ฅ์น˜์˜ ํ•ฉ์ด ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๋Šฅ๋ ฅ์น˜๊ฐ€ 3๊ณผ 7์ธ ๋‘ ์‚ฌ์›์ด ๊ฐ™์ด ๊ณต๋ถ€ํ•˜๋ฉด ๋‘ ์‚ฌ์›์˜ ๋Šฅ๋ ฅ์น˜๊ฐ€ ๋ชจ๋‘ 10์ด ๋ฉ๋‹ˆ๋‹ค. ์„ ๋ฐœํ•œ 2์ธ์˜ ๊ต์œก์ด ๋๋‚˜๋ฉด ๋ฏผ์ˆ˜๋Š” ๋‹ค์‹œ 2์ธ์„ ์„ ๋ฐœํ•˜์—ฌ ๊ต์œก์„ ์ง„ํ–‰ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ ํ•œ๋ฒˆ ๋ฏผ์ˆ˜์—๊ฒŒ ์„ ๋ฐœ๋œ ์‚ฌ์›์ด ๋‹ค์‹œ ์„ ๋ฐœ๋  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฏผ์ˆ˜๊ฐ€ ๊ต์œกํ•œ ์‹ ์ž…์‚ฌ์›๋“ค์„ ์ œ์™ธํ•œ ๋‹ค๋ฅธ ์‹ ์ž…์‚ฌ์›๋“ค์˜ ๋Šฅ๋ ฅ์น˜๋Š” ๋ณ€ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๋ฏผ์ˆ˜๋Š” ์‚ฐ์—…์ŠคํŒŒ์ด์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ต์œก ํ›„ ๋ชจ๋“  ์‹ ์ž…์‚ฌ์›๋“ค์˜ ๋Šฅ๋ ฅ์น˜์˜ ํ•ฉ์„ ์ตœ์†Œํ™”ํ•˜๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์‹ ์ž…์‚ฌ์›๋“ค์˜ ๋Šฅ๋ ฅ์น˜๊ฐ€ ์ˆœ์„œ๋Œ€๋กœ 10, 3, 7, 2์ด๋ฉฐ, ๋ฏผ์ˆ˜๊ฐ€ 2๋ฒˆ ๊ต์œก์„ ํ•ด์•ผ ํ•œ๋‹ค๋ฉด, ์ฒซ ๋ฒˆ์งธ ๊ต์œก์€ ๋‘ ๋ฒˆ์งธ ์‚ฌ์›๊ณผ ๋„ค ๋ฒˆ์งธ ์‚ฌ์›์„ ์„ ๋ฐœํ•˜๋ฉด ๋‘ ์‚ฌ์›์˜ ๋Šฅ๋ ฅ์น˜๊ฐ€ 5๊ฐ€ ๋˜์–ด ์‹ ์ž…์‚ฌ์›๋“ค์˜ ๋Šฅ๋ ฅ์น˜๊ฐ€ 10, 5, 7, 5๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ๊ต์œก๋„ ๋‘ ๋ฒˆ์งธ ์‚ฌ์›๊ณผ ๋„ค ๋ฒˆ์งธ ์‚ฌ์›์„ ์„ ๋ฐœํ•˜๋ฉด ์‹ ์ž…์‚ฌ์›๋“ค์˜ ๋Šฅ๋ ฅ์น˜๋Š” ์ˆœ์„œ๋Œ€๋กœ 10, 10, 7, 10์ด ๋ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ๊ฐ€ ๊ต์œก ํ›„ ๋ชจ๋“  ์‹ ์ž…์‚ฌ์›๋“ค์˜ ๋Šฅ๋ ฅ์น˜์˜ ํ•ฉ์ด 37๋กœ ์ตœ์†Œ๊ฐ€ ๋˜๋Š” ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค.

์‹ ์ž…์‚ฌ์›๋“ค์˜ ๋Šฅ๋ ฅ์น˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ •์ˆ˜ ๋ฐฐ์—ด ability์™€ ๋ฏผ์ˆ˜๊ฐ€ ๊ต์œก์„ ์ง„ํ–‰ํ•ด์•ผ ํ•˜๋Š” ํšŸ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ •์ˆ˜ number๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๊ต์œก ํ›„ ๋ชจ๋“  ์‹ ์ž…์‚ฌ์›๋“ค์˜ ๋Šฅ๋ ฅ์น˜์˜ ํ•ฉ์˜ ์ตœ์†Ÿ๊ฐ’์„ return ํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•˜์„ธ์š”.

์ œํ•œ ์‚ฌํ•ญ

  • 2 ≤ ability์˜ ๊ธธ์ด ≤ 1,000,000
  • 1 ≤ ability์˜ ์›์†Œ ≤ 100
  • 1 ≤ number ≤ 10,000
  • return ๊ฐ’์ด 10์–ต ์ดํ•˜๊ฐ€ ๋˜๋„๋ก ability์™€ number๊ฐ€ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

ability number result
[10, 3, 7, 2] 2 37
[1, 2, 3, 4] 3 26

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

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

  • ๋ฌธ์ œ ์˜ˆ์‹œ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

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

  • ์ฒซ ๋ฒˆ์งธ ์‚ฌ์›๊ณผ ๋‘ ๋ฒˆ์งธ ์‚ฌ์›์„ ์„ ๋ฐœํ•˜์—ฌ ๊ณต๋ถ€๋ฅผ ์‹œํ‚จ ํ›„, ์„ธ ๋ฒˆ์งธ ์‚ฌ์›๊ณผ ๋„ค ๋ฒˆ์งธ ์‚ฌ์›์„ ์„ ๋ฐœํ•˜์—ฌ ๊ณต๋ถ€๋ฅผ ์‹œํ‚ต๋‹ˆ๋‹ค. ๊ทธ ํ›„ ์ฒซ ๋ฒˆ์งธ ์‚ฌ์›๊ณผ ๋‘ ๋ฒˆ์งธ ์‚ฌ์›์„ ํ•œ๋ฒˆ ๋” ์„ ๋ฐœํ•ด ๊ณต๋ถ€๋ฅผ ์‹œํ‚ค๋ฉด, ์‹ ์ž…์‚ฌ์›๋“ค์˜ ๋Šฅ๋ ฅ์น˜๋Š” ์ˆœ์„œ๋Œ€๋กœ 6, 6, 7, 7์ด ๋˜๊ณ , ์ด๋•Œ๊ฐ€ ๋Šฅ๋ ฅ์น˜์˜ ํ•ฉ์ด ์ตœ์†Œ์ธ 26์ด ๋ฉ๋‹ˆ๋‹ค.

ํ’€์ด

  • ์ด๊ฑด ability ๋ฐฐ์—ด์„ ์ž‘์€ ๊ฐ’์œผ๋กœ ์ •๋ ฌํ•˜๊ณ , ์ž‘์€ ๊ฐ’ ๋‘๊ฐœ๋ฅผ ๋นผ์„œ, ํ•ฉ์„ ๋‘๋ฒˆ ๋„ฃ์–ด์ฃผ๋Š” ์ผ์„ number ๋งŒํผ ๋ฐ˜๋ณตํ•˜๋ฉด ๋œ๋‹ค.
  • ๊ทผ๋ฐ ์šฐ์„ ์ˆœ์œ„ํ๋ฅผ ์“ฐ๋ฉด ๋„ฃ์„ ๋•Œ ๋ถ€ํ„ฐ ์ •๋ ฌ๋˜์–ด ์žˆ์œผ๋‹ˆ๊นŒ ์ •๋ ฌํ•  ํ•„์š”๊ฐ€ ์—†๊ณ , ๋„ฃ์–ด์ค„ ๋•Œ๋„ ์ž๋™์ •๋ ฌ์ด ๋œ๋‹ค.
  • ์†๋„๊ฐ€ ๋Š๋ฆฌ๋„ค. ํž™ํ ์“ธ ๊ฑธ ๊ทธ๋žฌ๋‚˜... ํž™ํ ๋ช…๋ น์–ด๊ฐ€ ๊ธธ์–ด์„œ ๊ท€์ฐฎ....
from queue import PriorityQueue

def solution(ability, number):
    answer = 0
    pq = PriorityQueue()
    
    for i in ability:
        pq.put(i)
    for i in range(number):
        a = pq.get()
        b = pq.get()
        pq.put(a+b)
        pq.put(a+b)
    
    while pq.empty() != True:
        answer += pq.get()
        
    return answer
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (983.50ms, 14.8MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (2973.13ms, 24.9MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (2994.13ms, 25.6MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (1391.92ms, 17.3MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (3322.82ms, 24.8MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (506.97ms, 12.8MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (650.27ms, 13MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (2682.30ms, 19.8MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (3598.64ms, 24.9MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (832.36ms, 13.4MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (3692.37ms, 27MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (3764.67ms, 27MB)
  • ํž™ํ ๋ช…๋ น์–ด ๊ธด๊ฑด ์ด๋ ‡๊ฒŒ ์ค„์ผ ์ˆ˜ ์žˆ๊ธด ํ•˜๊ตฌ๋‚˜...
  • ํž™ํ๋กœ ๋ฐ”๊พธ๋‹ˆ๊นŒ ๊ฐœ๋นจ๋ผ์ง€๋„ค ใ…‹ใ…‹ใ…‹
from heapq import heapify as heapify, heappush as push, heappop as pop

def solution(ability, number):
    heapify(ability)
    
    for i in range(number):
        a = pop(ability)
        b = pop(ability)
        push(ability, a + b)
        push(ability, a + b)
        
    return sum(ability)
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (12.13ms, 12.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (42.94ms, 17.5MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (41.95ms, 17.9MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (21.94ms, 13.4MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (46.49ms, 17.3MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (10.28ms, 11.3MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (13.80ms, 11.3MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (35.87ms, 15MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (36.66ms, 17.3MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (23.78ms, 11.7MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (41.90ms, 18.6MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (47.33ms, 18.6MB)
728x90
๋ฐ˜์‘ํ˜•