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

The only one you can truly trust is yourself.

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

[PCCP ๋ชจ์˜๊ณ ์‚ฌ#1] ์ฒด์œก๋Œ€ํšŒ (itertools.permutation)

๐ŸŽฎinspirer9 2023. 3. 6. 23:20
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ์„ค๋ช…

๋‹น์‹ ์ด ๋‹ค๋‹ˆ๋Š” ํ•™๊ต๋Š” ๋งค๋…„ ์ฒด์œก๋Œ€ํšŒ๋ฅผ ํ•ฉ๋‹ˆ๋‹ค. ์ฒด์œก๋Œ€ํšŒ๋Š” ์—ฌ๋Ÿฌ ์ข…๋ชฉ์— ๋Œ€ํ•ด ๊ฐ ๋ฐ˜์˜ ํ•ด๋‹น ์ข…๋ชฉ ๋Œ€ํ‘œ๊ฐ€ 1๋ช…์”ฉ ๋‚˜์™€ ๋Œ€๊ฒฐ์„ ํ•˜๋ฉฐ, ํ•œ ํ•™์ƒ์€ ์ตœ๋Œ€ ํ•œ๊ฐœ์˜ ์ข…๋ชฉ ๋Œ€ํ‘œ๋งŒ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹น์‹ ์˜ ๋ฐ˜์—์„œ๋„ ํ•œ ์ข…๋ชฉ๋‹น 1๋ช…์˜ ๋Œ€ํ‘œ๋ฅผ ๋ฝ‘์œผ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ํ•™์ƒ๋“ค๋งˆ๋‹ค ๊ฐ ์ข…๋ชฉ์— ๋Œ€ํ•œ ๋Šฅ๋ ฅ์ด ๋‹ค๋ฅด์ง€๋งŒ ์ด ๋Šฅ๋ ฅ์€ ์ˆ˜์น˜ํ™”๋˜์–ด ์žˆ์–ด ๋ฏธ๋ฆฌ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹น์‹ ์˜ ๋ฐ˜์˜ ์ „๋žต์€ ๊ฐ ์ข…๋ชฉ ๋Œ€ํ‘œ์˜ ํ•ด๋‹น ์ข…๋ชฉ์— ๋Œ€ํ•œ ๋Šฅ๋ ฅ์น˜์˜ ํ•ฉ์„ ์ตœ๋Œ€ํ™”ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๋‹ค์Œ์€ ๋‹น์‹ ์˜ ๋ฐ˜ ํ•™์ƒ์ด 5๋ช…์ด๊ณ , ์ข…๋ชฉ์˜ ๊ฐœ์ˆ˜๊ฐ€ 3๊ฐœ์ด๋ฉฐ, ๊ฐ ์ข…๋ชฉ์— ๋Œ€ํ•œ ํ•™์ƒ๋“ค์˜ ๋Šฅ๋ ฅ์น˜๊ฐ€ ์•„๋ž˜ ํ‘œ์™€ ๊ฐ™์„ ๋•Œ, ๊ฐ ์ข…๋ชฉ์˜ ๋Œ€ํ‘œ๋ฅผ ๋ฝ‘๋Š” ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค.

  ํ…Œ๋‹ˆ์Šค ํƒ๊ตฌ ์ˆ˜์˜
์„ํ™˜ 40 10 10
์˜์žฌ 20 5 0
์ธ์šฉ 30 30 30
์ •ํ˜„ 70 0 70
์ค€๋ชจ 100 100 100

ํ…Œ๋‹ˆ์Šค ๋Œ€ํ‘œ๋กœ ์ค€๋ชจ, ํƒ๊ตฌ ๋Œ€ํ‘œ๋กœ ์ธ์šฉ, ์ˆ˜์˜ ๋Œ€ํ‘œ๋กœ ์ •ํ˜„์„ ๋ฝ‘๋Š”๋‹ค๋ฉด, ์„ธ ๋ช…์˜ ๊ฐ ์ข…๋ชฉ์— ๋Œ€ํ•œ ๋Šฅ๋ ฅ์น˜์˜ ํ•ฉ์€ 200(=100+30+70)์ด ๋ฉ๋‹ˆ๋‹ค.
ํ•˜์ง€๋งŒ, ํ…Œ๋‹ˆ์Šค ๋Œ€ํ‘œ๋กœ ์„ํ™˜, ํƒ๊ตฌ ๋Œ€ํ‘œ๋กœ ์ค€๋ชจ, ์ˆ˜์˜ ๋Œ€ํ‘œ๋กœ ์ •ํ˜„์„ ๋ฝ‘๋Š”๋‹ค๋ฉด ์„ธ ๋ช…์˜ ๊ฐ ์ข…๋ชฉ์— ๋Œ€ํ•œ ๋Šฅ๋ ฅ์น˜ ํ•ฉ์€ 210(=40+100+70)์ด ๋ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ๊ฐ€ ๋‹น์‹ ์˜ ๋ฐ˜์˜ ๊ฐ ์ข…๋ชฉ ๋Œ€ํ‘œ์˜ ๋Šฅ๋ ฅ์น˜ ํ•ฉ์ด ์ตœ๋Œ€๊ฐ€ ๋˜๋Š” ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค.

๋‹น์‹ ์˜ ๋ฐ˜ ํ•™์ƒ๋“ค์˜ ๊ฐ ์ข…๋ชฉ์— ๋Œ€ํ•œ ๋Šฅ๋ ฅ์น˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” 2์ฐจ์› ์ •์ˆ˜ ๋ฐฐ์—ด ability๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์„ ๋ฐœ๋œ ๋Œ€ํ‘œ๋“ค์˜ ํ•ด๋‹น ์ข…๋ชฉ์— ๋Œ€ํ•œ ๋Šฅ๋ ฅ์น˜ ํ•ฉ์˜ ์ตœ๋Œ€๊ฐ’์„ return ํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•˜์‹œ์˜ค.

์ œํ•œ ์‚ฌํ•ญ

  • 1 ≤ ability์˜ ํ–‰์˜ ๊ธธ์ด = ํ•™์ƒ ์ˆ˜ ≤ 10
  • 1 ≤ ability์˜ ์—ด์˜ ๊ธธ์ด = ์ข…๋ชฉ ์ˆ˜ ≤ ability์˜ ํ–‰์˜ ๊ธธ์ด
  • 0 ≤ ability[i][j] ≤ 10,000
    • ability[i][j]๋Š” i+1๋ฒˆ ํ•™์ƒ์˜ j+1๋ฒˆ ์ข…๋ชฉ์— ๋Œ€ํ•œ ๋Šฅ๋ ฅ์น˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

ability result
[[40, 10, 10], [20, 5, 0], [30, 30, 30], [70, 0, 70], [100, 100, 100]] 210
[[20, 30], [30, 20], [20, 30]] 60

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

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

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

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

  • 1๋ฒˆ ํ•™์ƒ์ด 2๋ฒˆ ์ข…๋ชฉ์„, 2๋ฒˆ ํ•™์ƒ์ด 1๋ฒˆ ์ข…๋ชฉ์˜ ๋Œ€ํ‘œ๋กœ ์ฐธ๊ฐ€ํ•˜๋Š” ๊ฒฝ์šฐ์— ๋Œ€ํ‘œ๋“ค์˜ ํ•ด๋‹น ์ข…๋ชฉ์— ๋Œ€ํ•œ ๋Šฅ๋ ฅ์น˜์˜ ํ•ฉ์ด ์ตœ๋Œ€๊ฐ€ ๋˜๋ฉฐ, ์ด๋Š” 60์ž…๋‹ˆ๋‹ค.

ํ’€์ด

  • ๋ฌด์ง€์„ฑ ์ฝ”๋”ฉ์˜ ์œ ํ˜น์„ ์ด๊ธฐ์ง€ ๋ชปํ•˜๊ณ  ๊ทธ๋ƒฅ ๋ณธ๋Šฅ์ด ์ด๋„๋Š”๋Œ€๋กœ ํ’€์–ด๋ฒ„๋ ธ๋‹ค.
  • ํ•™์ƒ์ˆ˜์™€ ์ข…๋ชฉ์ˆ˜๋กœ ์กฐํ•ฉ์„ ๋งŒ๋“ค๊ณ , ์กฐํ•ฉ์œผ๋กœ ๋งŒ๋“ ๊ฑธ๋กœ ์–ด๋นŒ๋ฆฌํ‹ฐ ํ•ฉ์‚ฐํ•ด์„œ ๋” ํฌ๋ฉด ๊ต์ฒด.
  • ๊ทธ๋ ‡๊ฒŒ ๋ฃจํ”„๊ฐ€ ๋๋‚˜๋ฉด ๋Šฅ๋ ฅ์น˜ ํ•ฉ์˜ ์ตœ๋Œ€๊ฐ’์ด answer์— ๋‚จ๊ฒŒ ๋œ๋‹ค.
import itertools

def solution(ability):
    answer = 0
    ํ•™์ƒ์ˆ˜ = len(ability)
    ์ข…๋ชฉ์ˆ˜ = len(ability[0])
    ์กฐํ•ฉ = itertools.permutations([i for i in range(ํ•™์ƒ์ˆ˜)], ์ข…๋ชฉ์ˆ˜)
    
    for i in ์กฐํ•ฉ:
        _tmp = 0
        index = 0
        for j in i:
            _tmp += ability[j][index]
            index += 1
        if _tmp > answer:
            answer = _tmp
            
    return answer
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (85.10ms, 10.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (26.23ms, 10.3MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (3388.17ms, 10.4MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (29.24ms, 10.4MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.71ms, 10.4MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.5MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.35ms, 10.5MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (3520.79ms, 10.5MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (31.01ms, 10.4MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (57.89ms, 10.5MB)
728x90
๋ฐ˜์‘ํ˜•