-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaxChange.java
27 lines (26 loc) · 1.01 KB
/
MaxChange.java
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
import java.io.*;
import java.lang.Integer;
import java.util.Arrays;
public class MaxChange {
public static void main(String[] args) throws IOException {
//http://people.cs.clemson.edu/~bcdean/dp_practice/dp_2.swf
int[] A = {1, 5, 10, 25}; // =
int N = 63;
System.out.println(ans(A, N));
}
public static int ans(int[] ar, int W) {
int[] dp = new int[W+1];
for (int i = 0; i < ar.length; i++)
dp[ar[i]] = 1;
for (int i = 1; i < dp.length; i++) {
int min = Integer.MAX_VALUE;
for (int j = 0; j < ar.length && ar[j] <= i; j++)
if(i-ar[j] >= 0 && dp[i-ar[j]] < min)
min = dp[i-ar[j]];//by finding min num for i-ar[j], you can add the jth coin to get the value i
//ex: dp[63-25] = dp[38] --> min coins to get 38, + a 25 coin = 63
//subproblems = MinCoins[i] is minimum number of coins to get to value i
dp[i] = min + 1;
}
return dp[W];
}
}