๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Python/๋ฐฑ์ค€

๋ฐฑ์ค€ 2293๋ฒˆ : ๋™์ „ 1

by coderSohyun 2023. 8. 17.

 

 

 

๐Ÿ‘‰ ์ œ์ถœ ์ฝ”๋“œ

import sys

input = sys.stdin.readline

n, k = map(int, input().split())
c = [int(input()) for i in range(n)]

dp = [0 for i in range(k+1)]
dp[0] = 1

for i in c:
    for j in range(i, k+1):
        if j-i >= 0:
            dp[j] += dp[j-i]

print(dp[k])

 

ํ•ต์‹ฌ๋ฝ€์ธ๋œจ : 

* ๋™์  ๊ณ„ํš๋ฒ• (Dynamic Programming, DP) ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ ๊ณ ๋ คํ•ด์•ผํ•  ๊ฒƒ๋“ค 

  • '์ „์ฒด ๋ฌธ์ œ'๋ฅผ '๋ถ€๋ถ„ ๋ฌธ์ œ'๋กœ ์ž˜ ๋‚˜๋ˆ ์„œ ๋ฌธ์ œ๋ฅผ ์ ‘๊ทผํ–ˆ๋Š”๊ฐ€?
  • ์ „์ฒด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•œ ๋ถ€๋ถ„ ๋ฌธ์ œ์˜ ์ ํ™”์‹์€ ๋ฌด์—‡์ธ๊ฐ€?
  • ๋ถ€๋ถ„ ๋ฌธ์ œ๋“ค์„ ํ•ด๊ฒฐ ํ›„ ์–ป์€ ๊ฒฐ๊ณผ๊ฐ’๋“ค์„ ๋ฉ”๋ชจ์ด์ œ์ด์…˜ํ•˜์˜€๋Š”๊ฐ€?
  • ๋ถ€๋ถ„ ๋ฌธ์ œ์˜ ์ ํ™”์‹์€ ๋ถ€๋ถ„ ๋ฌธ์ œ๋“ค ์‚ฌ์ด์˜ '๊ด€๊ณ„'๋ฅผ ๋น ์ง์—†์ด ๊ณ ๋ คํ–ˆ๋Š”๊ฐ€?

 

๋ฌธ์ œ๋ฅผ ํ†ตํ•ด์„œ ์•Œ ์ˆ˜ ์žˆ๋Š” ์  : 

* ์•ž์„œ ์‚ดํŽด๋ณธ ๋™์  ๊ณ„ํš๋ฒ• ๊ณ ๋ ค์‚ฌํ•ญ๋“ค์„ ๋ฐ”ํƒ•์œผ๋กœ ๋ฌธ์ œ๋ฅผ ๋ฐ”๋ผ๋ด๋ณด์ž!

  • '์ „์ฒด ๋ฌธ์ œ'๋ฅผ '๋ถ€๋ถ„ ๋ฌธ์ œ'๋กœ ์ž˜ ๋‚˜๋ˆ ์„œ ๋ฌธ์ œ๋ฅผ ์ ‘๊ทผํ–ˆ๋Š”๊ฐ€?
    • ์ „์ฒด ๋ฌธ์ œ : ๊ฐ€์น˜์˜ ํ•ฉ์ด k์›์ด ๋˜๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜
    • ๋ถ€๋ถ„ ๋ฌธ์ œ : ๊ฐ€์น˜์˜ ํ•ฉ์ด i (1 ≤ i ≤ k)์›์ด ๋˜๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜ 
      • (๋ถ€๋ถ„ ๋ฌธ์ œ๋ฅผ ๋” ์„ธ๋ถ€์ ์œผ๋กœ ๋‚˜๋ˆ ๋ณด์ž๋ฉด) ํŠน์ • ๋™์ „์„ ์ผ์„ ๋•Œ ๊ฐ€์น˜์˜ ํ•ฉ์ด i์›์ด ๋˜๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜ 
  • ์ „์ฒด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•œ ๋ถ€๋ถ„ ๋ฌธ์ œ์˜ ์ ํ™”์‹์€ ๋ฌด์—‡์ธ๊ฐ€?
  • ๋ถ€๋ถ„ ๋ฌธ์ œ๋“ค์„ ํ•ด๊ฒฐ ํ›„ ์–ป์€ ๊ฒฐ๊ณผ๊ฐ’๋“ค์„ ๋ฉ”๋ชจ์ด์ œ์ด์…˜ํ•˜์˜€๋Š”๊ฐ€?
    • ์•ž์„œ ์–ธ๊ธ‰ํ•œ ๋ถ€๋ถ„ ๋ฌธ์ œ๋“ค์„ ํ•ด๊ฒฐํ•ด๋‚˜๊ฐ€๋ฉฐ ๋ฉ”๋ชจ์ด์ œ์ด์…˜ ํ•  ๊ฒƒ์ธ๋ฐ, ์‹œ๊ฐ„ ์ œํ•œ์ด 0.5์ดˆ์ด๋ฉฐ ๋ฉ”๋ชจ๋ฆฌ ์ œํ•œ๋„ 4MB๋ฐ–์— ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ํ•˜๋‚˜์˜ ๋ฆฌ์ŠคํŠธ ์•ˆ์—์„œ ๋ฎ์–ด ์”Œ์šฐ๋Š” ์‹์œผ๋กœ ๋น ๋ฅด๊ฒŒ ํ•ด๊ฒฐํ•ด๋‚˜๊ฐ€์•ผ ํ•จ
  • ๋ถ€๋ถ„ ๋ฌธ์ œ์˜ ์ ํ™”์‹์€ ๋ถ€๋ถ„ ๋ฌธ์ œ๋“ค ์‚ฌ์ด์˜ '๊ด€๊ณ„'๋ฅผ ๋น ์ง์—†์ด ๊ณ ๋ คํ–ˆ๋Š”๊ฐ€?
    • ์ž…์ถœ๋ ฅ ์˜ˆ์‹œ๋กœ ์ฃผ์–ด์ง„ ๊ฒฝ์šฐ ๋ง๊ณ ๋„ ๋‹ค๋ฅธ ์˜ˆ์‹œ๋ฅผ ์ƒ๊ฐํ•ด๋ณด๋ฉฐ ํ…Œ์ŠคํŒ…ํ•ด๋ด์•ผ ํ•  ๋“ฏํ•จ 

 

ํ•„์š”ํ•œ ์ž…๋ ฅ๊ฐ’ :

 

 

์ถœ๋ ฅ๊ฐ’:

 

 

 

๐Ÿ‘‰ ํŒŒ์•…ํ•œ ๊ฒƒ์„ ๋ฐ”ํƒ•์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด๋ณด์ž!

 

 

 

๐Ÿ‘‰ ์•Œ๋ฉด ์ข‹์€ ์ฝ”๋“œ๋“ค

๐Ÿ“๋™์  ๊ณ„ํš๋ฒ• ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ ๊ฟ€ํŒ์ด ์—†์„๊นŒ? ํ•˜๊ณ  ๊ตฌ๊ธ€๋ง์„ ์—„์ฒญ ํ–ˆ์—ˆ๋Š”๋ฐ, ๊ทธ ์ค‘์—์„œ ์ œ์ผ ๊น”๋”ํ•˜๊ฒŒ ์ •๋ฆฌ๊ฐ€ ์ž˜ ๋˜์–ด ์žˆ๋˜ ๋ธ”๋กœ๊ทธ์˜€์Œ! 

(์ฐธ๊ณ ) https://mong9data.tistory.com/68

'Python > ๋ฐฑ์ค€' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

๋ฐฑ์ค€ 11650๋ฒˆ : ์ขŒํ‘œ ์ •๋ ฌํ•˜๊ธฐ (python)  (2) 2023.05.19