-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMinimizingCoins.java
More file actions
36 lines (32 loc) · 1013 Bytes
/
MinimizingCoins.java
File metadata and controls
36 lines (32 loc) · 1013 Bytes
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
import java.io.*;
import java.util.*;
public class MinimizingCoins {
static long max = 1000000000;
public static void main (String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s[] = br.readLine().split(" ");
int n = Integer.parseInt(s[0]);
int x = Integer.parseInt(s[1]);
String s1[] = br.readLine().split(" ");
int a[] = new int[n];
for(int i = 0; i < n; i++) {
a[i] = Integer.parseInt(s1[i]);
}
long dp[] = new long[x+1];
Arrays.fill(dp, max);
dp[0] = 0;
for(int i = 1; i <= x; i++) {
for(int coin: a) {
if(i >= coin) {
dp[i] = Math.min(dp[i], dp[i-coin] + 1);
}
}
}
if(dp[x] == max) {
System.out.println(-1);
} else {
System.out.println(dp[x]);
}
}
}