[๋ฐฑ์ค€ ๐Ÿฅ‡5] 22115๋ฒˆ ์ฐฝ์˜์ด์™€ ์ปคํ”ผ (Python/ํŒŒ์ด์ฌ)

mingssoยท2023๋…„ 3์›” 19์ผ
0

Algorithm

๋ชฉ๋ก ๋ณด๊ธฐ
24/35

1๏ธโƒฃ ๋ฌธ์ œ

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



2๏ธโƒฃ ์ฝ”๋“œ

๊ทธ๋™์•ˆ ๋ด์˜ค๋˜ ๋ฐฐ๋‚ญ ๋ฌธ์ œ์™€ ์œ ํ˜•์ด ์•ฝ๊ฐ„ ๋‹ฌ๋ผ์„œ ํ—ค๋งธ๋‹ค๐Ÿ˜ญ
์•„์ง ๋ฐฐ๋‚ญ๋ฌธ์ œ๋ฅผ ์™„์ „ํžˆ ์ดํ•ดํ•˜์ง€ ๋ชปํ–ˆ๋‚˜๋ณด๋‹ค

n, k = map(int, input().split())
c = [0] + list(map(int, input().split()))

dp = [[1000000] * (k + 1) for _ in range(n+1)]   # dp[i][j] = i๋ฒˆ์งธ ์ปคํ”ผ๊นŒ์ง€ ํ™•์ธํ–ˆ์„ ๋•Œ ์นดํŽ˜์ธ ์–‘์ด j์ธ ์ปคํ”ผ์˜ ์ตœ์†Œ ๊ฐœ์ˆ˜
for i in range(n+1):
    dp[i][0] = 0

for i in range(1, n+1):
    for j in range(1, k+1):
        if j < c[i]:
            dp[i][j] = dp[i-1][j]
        else:
            dp[i][j] = min(dp[i-1][j], dp[i-1][j-c[i]] + 1)

if dp[n][k] == 1000000:
    print(-1)
else:
    print(dp[n][k])
profile
๐Ÿฅ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ’ฐ

0๊ฐœ์˜ ๋Œ“๊ธ€