๋ฌธ์
https://www.acmicpc.net/problem/2294
๋ด ๋ฌธ์ ํ์ด
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class BOJ_2294 {
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
st = new StringTokenizer(in.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
int[] coin = new int[n];
for(int i = 0; i < n; i++) {
coin[i] = Integer.parseInt(in.readLine());
}
int[] memory = new int[k+1];
Arrays.fill(memory, 100001);
memory[0] = 0;
for(int c: coin) {
for(int i = c; i < k+1; i++) {
memory[i] = Math.min(memory[i], memory[i - c] + 1);
}
}
System.out.println(memory[k] == 100001 ? -1 : memory[k]);
}
}
๐ DP ๋ฌธ์
์ ํ์ : memory[i] = Math.min(memory[i], memory[i - c] + 1);
โ๏ธ ๋ฐ๋ก ์ถ์ฒ
2 100
7
8
๐ก ํผ๋๋ฐฑ
- DP๋ ํญ์ ์ ํ์ ๊ตฌํ๋๊ฒ ํ๋ ๋ฐ..!! ์ด๋ฒ์๋ ์ฒ์์ ํ๋ค๋ฆฌ ์ง์ด์ ์์ฒญ ์ค๋๊ฑธ๋ ธ๋ค.ใ
'2๏ธโฃ Java > Problem Solving' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java Algorithm] ์ธ๊ตฌ ์ด๋ BOJ #16234 (0) | 2022.09.22 |
---|---|
[Java Algorithm] ์ต์ ํ์์ค ๊ฐ์ BOJ #19598 (0) | 2022.08.31 |
[Java Algorithm] ABCDE BOJ #13023 (0) | 2022.08.31 |
[Java Algorithm] ์ ๋ก์์ฝ BOJ #10026 (0) | 2022.08.31 |
[Java Algorithm] ํ์ถ BOJ #3055 (0) | 2022.08.31 |
๋๊ธ