728x90
SMALL
DP ๋ฌธ์
https://www.acmicpc.net/problem/2293
๐ก ํ์ด์ฝ๋ (์คํจ - ๋ฉ๋ชจ๋ฆฌ ์ด๊ณผ)
import sys
n, k = map(int, sys.stdin.readline().split())
coin = [0]
for _ in range(n):
num = int(sys.stdin.readline())
coin.append(num)
def sol(n, k, coin):
dp = [[0] * (k + 1) for _ in range(n + 1)]
for i in range(n + 1):
dp[i][0] = 1
for i in range(1, n + 1):
for j in range(1, k + 1):
if j - coin[i] < 0:
dp[i][j] = dp[i - 1][j]
else:
dp[i][j] = dp[i - 1][j] + dp[i][j - coin[i]]
print(dp[n][k])
sol(n, k, coin)
๐ก ํ์ด์ฝ๋ (์ฑ๊ณต)
import sys
n, k = map(int, sys.stdin.readline().split())
coin = [0]
for _ in range(n):
num = int(sys.stdin.readline())
coin.append(num)
def sol(n, k, coin):
dp = [0] * (k + 1)
dp[0] = 1
for i in range(1, n + 1):
for j in range(1, k + 1):
if j - coin[i] >= 0:
dp[j] += dp[j - coin[i]]
print(dp[k])
sol(n, k, coin)
2์ฐจ์ ๋ฐฐ์ด -> 1์ฐจ์ ๋ฐฐ์ด๋ก ๋ณ๊ฒฝํด์ฃผ๋ ๋ฉ๋ชจ๋ฆฌ ์ด๊ณผ ๋ฌธ์ ๊ฐ ํด๊ฒฐ๋์๋ค.
728x90
LIST
'Algorithm > DP' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] 15989 : 1, 2, 3 ๋ํ๊ธฐ 4 (ํ์ด์ฌ) (0) | 2024.08.23 |
---|---|
[๋ฐฑ์ค] 9095 : 1, 2, 3 ๋ํ๊ธฐ (ํ์ด์ฌ) (0) | 2024.08.23 |
[๋ฐฑ์ค] 17404 : RGB ๊ฑฐ๋ฆฌ 2 (ํ์ด์ฌ) (0) | 2024.08.12 |
[๋ฐฑ์ค] 1149 : RGB ๊ฑฐ๋ฆฌ (ํ์ด์ฌ) (0) | 2024.08.12 |
[๋ฐฑ์ค] 10986 : ๋๋จธ์ง ํฉ (ํ์ด์ฌ) (0) | 2024.08.08 |