forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path8.java
More file actions
41 lines (34 loc) Β· 1.17 KB
/
8.java
File metadata and controls
41 lines (34 loc) Β· 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// μ μ N, Mμ μ
λ ₯λ°κΈ°
int n = sc.nextInt();
int m = sc.nextInt();
// Nκ°μ νν λ¨μ μ 보λ₯Ό μ
λ ₯ λ°κΈ°
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
// μμ κ³μ°λ κ²°κ³Όλ₯Ό μ μ₯νκΈ° μν DP ν
μ΄λΈ μ΄κΈ°ν
int[] d = new int[m + 1];
Arrays.fill(d, 10001);
// λ€μ΄λλ―Ή νλ‘κ·Έλλ°(Dynamic Programming) μ§ν(보ν
μ
)
d[0] = 0;
for (int i = 0; i < n; i++) {
for (int j = arr[i]; j <= m; j++) {
// (i - k)μμ λ§λλ λ°©λ²μ΄ μ‘΄μ¬νλ κ²½μ°
if (d[j - arr[i]] != 10001) {
d[j] = Math.min(d[j], d[j - arr[i]] + 1);
}
}
}
// κ³μ°λ κ²°κ³Ό μΆλ ₯
if (d[m] == 10001) { // μ΅μ’
μ μΌλ‘ Mμμ λ§λλ λ°©λ²μ΄ μλ κ²½μ°
System.out.println(-1);
}
else {
System.out.println(d[m]);
}
}
}