๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
4๏ธโƒฃ Python/Problem Solving

[Python Algorithm] ๋™์ „1 BOJ #2293

by seolhee2750 2022. 5. 26.
๋ฌธ์ œ

https://www.acmicpc.net/problem/2293

 

2293๋ฒˆ: ๋™์ „ 1

์ฒซ์งธ ์ค„์— n, k๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ n ≤ 100, 1 ≤ k ≤ 10,000) ๋‹ค์Œ n๊ฐœ์˜ ์ค„์—๋Š” ๊ฐ๊ฐ์˜ ๋™์ „์˜ ๊ฐ€์น˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋™์ „์˜ ๊ฐ€์น˜๋Š” 100,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค.

www.acmicpc.net

 

๋‚ด ๋ฌธ์ œ ํ’€์ด
import sys

n, k = map(int, sys.stdin.readline().strip().split())
coin = []
for _ in range(n):
    coin.append(int(sys.stdin.readline().strip()))
dp = [0 for _ in range(k+1)]
dp[0] = 1

for c in coin:
    for i in range(c, k+1):
        dp[i] += dp[i - c]

print(dp[-1])

๐Ÿ‘‰ DP ๋ฌธ์ œ

์ ํ™”์‹์„ ๊ตฌํ•˜๋Š” ๊ณผ์ •์„ ์•„๋ž˜ ๊ทธ๋ฆผ์œผ๋กœ ํ‘œํ˜„ํ–ˆ๋‹ค.

๋Œ€ํ‘œ๋กœ 1, 2, 5 ๋™์ „์„ ๋ชจ๋‘ ์‚ฌ์šฉํ•˜์—ฌ '6'์„ ๋งŒ๋“œ๋Š” ๊ณผ์ •์„ ์„œ์ˆ ํ–ˆ๋‹ค.

6์˜ ๊ฒฝ์šฐ (1, 1, 1, 1, 1), (1, 1, 1, 1, 2), (1, 1, 2, 2), (2, 2, 2), (1, 5) ์ด๋ ‡๊ฒŒ 5๊ฐ€์ง€ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ฐ€์ง€๋Š”๋ฐ,

์ด๋Š” ๊ธฐ์กด์— 1๊ณผ 2๋งŒ ๊ฐ€์ง€๊ณ  6์„ ๋งŒ๋“ค์—ˆ๋˜ ๊ฒฝ์šฐ์˜ ์ˆ˜์—, '6 - 5' ์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋”ํ•ด์ฃผ๋ฉด ๋˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

=> dp[i] += dp[i - c]

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ

์ ํ™”์‹ ๋„์ถœ ๊ณผ์ •์—์„œ,, ์•Œ๋“ฏ ๋ง๋“ฏ ํ•˜์—ฌ ์•„์ฃผ ์˜ค๋ž˜๊ฑธ๋ ธ๋‹ค.

๋งŽ์ด ํ’€๋ฉด ๋‚˜์•„์ง€๊ฒ ์ง€ใ…ก,?,,

๋Œ“๊ธ€