[BOJ 2294] ๋์ 2
1. ๋ฌธ์ : Link
๋์ 1๊ณผ ๋น์ทํ์ง๋ง ์ต์๊ฐ์ ๊ตฌํ๋ ๋ฌธ์
2. ํ์ด
dp[0] = 0
๊ฐ dp ๊ฐ์ ํด๋น ๊ฐ์์ - ์ฝ์ธ ํ ๊ฐ + 1 (๊ฐ์๊ฐ ํ๋ ๋์ด๋๋๊ฒ)
๊ธฐ์กด dp ๊ฐ๊ณผ ๋น๊ตํด์ min๊ฐ ๋ฃ์ด์ฃผ๊ธฐ
10001 (์ต๋๊ฐ)์ด๋ผ๋ฉด -1 ์ถ๋ ฅ
3. ์ฝ๋
import java.io.IOException;
import java.util.*;
public class Main {
static int N;
static int sum;
static int[] coins;
static int[] dp;
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
sum = sc.nextInt();
coins = new int[N];
for(int i=0; i<N; i++){
coins[i] = sc.nextInt();
}
dp = new int[sum+1];
Arrays.fill(dp, 10001);
dp[0] = 0;
for(int i = 0; i < N; i++) {
for(int j = coins[i]; j <= sum; j++) {
dp[j] = Math.min(dp[j],dp[j - coins[i]]+1);
}
}
if(dp[sum] == 10001) System.out.println(-1);
else System.out.println(dp[sum]);
}
}
'๐ป Coding Problems Solving > Dynamic Programming' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ๋ฉ๋ฆฌ ๋ฐ๊ธฐ (์๋ฐ java) (0) | 2023.01.12 |
---|---|
[LeetCode] Maximum Subarray (0) | 2022.07.24 |
[LeetCode] Climbing Stairs (0) | 2022.07.09 |
[BOJ 15486] ํด์ฌ2 (0) | 2022.07.05 |
[BOJ 2293] ๋์ 1 (0) | 2022.06.29 |
์ต๊ทผ๋๊ธ