Search
Duplicate
๐Ÿฅˆ

๋™์ „ 2

์ฃผ์ฐจ
13
๋ฌธ์ œ๋ฒˆํ˜ธ
2294
์–ธ์–ด
ํ‹ฐ์–ด
์‹ค๋ฒ„
์œ ํ˜•
DP
nj_Blog
O
nj_์ƒํƒœ
์™„๋ฃŒ
์ดํ•ด๋„
ํ’€์ด
์‚ฌ๋žŒ
์ดํ•ด๋„ 2
13 more properties

๋ฌธ์ œ๋งํฌ

์ฝ”๋“œ ์ œ์ถœ ๊ธฐ๋ก (๋ฉ”๋ชจ๋ฆฌ ๋ฐ ์‹œ๊ฐ„)

๋ฉ”๋ชจ๋ฆฌ : 29028KB
์‹œ๊ฐ„ : 320ms

Code

import sys input = sys.stdin.readline def nj(k, coin, dp): temp=[] for i in coin: if k >= i and dp[k-i] != -1: temp.append(dp[k-i]+1) if len(temp)>0: return min(temp) else: return -1 n, k = map(int, input().split()) coin=[] for i in range(n): coin.append(int(input())) coin.sort() dp = [-1 for i in range(k + 1)] for i in range(1, k+1): if i in coin: dp[i] = 1 else: dp[i] = nj(i, coin, dp) print(int(dp[k]))
Python
๋ณต์‚ฌ

๋ฉ”๋ชจ

<ํ‘ธ๋Š” ์ˆœ์„œ>

1) coin ๋ฐฐ์—ด์˜ ์›์†Œ == dp ์˜ ์ธ๋ฑ์Šค ์ธ ๋ถ€๋ถ„์€ ํ•ญ์ƒ 1๊ฐœ์ด๋ฏ€๋กœ dp[x] = 1
2) coin ๋ฐฐ์—ด์˜ ์›์†Œ != dp ์˜ ์ธ๋ฑ์Šค ์ธ ๋ถ€๋ถ„์€ dp[x-coin์˜ ์›์†Œ๋“ค]+1 ์˜ ์ตœ์†Ÿ๊ฐ’์„ ๋„ฃ๋Š”๋‹ค

<python : ์ •์ˆ˜ ๋‘๊ฐœ ์ž…๋ ฅ๋ฐ›๊ธฐ>

import sys input = sys.stdin.readline n, k = map(int, input().split())
Python
๋ณต์‚ฌ

<python : ํฌ๊ธฐ n ๋ฐฐ์—ด ์ž…๋ ฅ๋ฐ›๊ธฐ>

import sys input = sys.stdin.readline arr=[] for i in range(n): arr.append(int(input()))
Python
๋ณต์‚ฌ

<python : ๋ฐฐ์—ด ์˜ค๋ฆ„์ฐจ์ˆœ/๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•˜๊ธฐ>

arr.sort() #์˜ค๋ฆ„์ฐจ์ˆœ arr.reverse() #๋‚ด๋ฆผ์ฐจ์ˆœ
Python
๋ณต์‚ฌ