-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDP:CoinChange.java
More file actions
35 lines (30 loc) · 931 Bytes
/
Copy pathDP:CoinChange.java
File metadata and controls
35 lines (30 loc) · 931 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
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static long makeChange(int[] coins, int money) {
long[] counts = new long[money + 1];
for (int i = 0; i < money + 1; i++) {
counts[i] = 0;
}
counts[0] = 1;
for (int i =0; i < coins.length; i++) {
for (int j = coins[i]; j < money + 1; j++) {
counts[j] += counts[j - coins[i]];
}
}
return counts[money];
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
int coins[] = new int[m];
for(int coins_i=0; coins_i < m; coins_i++){
coins[coins_i] = in.nextInt();
}
System.out.println(makeChange(coins, n));
}
}