package chapter_4_recursionanddp;
public class Problem_03_CoinsMin {
public static int minCoins1(int[] arr, int aim) {
if (arr == null || arr.length == 0 || aim < 0) {
return -1;
}
int n = arr.length;
int max = Integer.MAX_VALUE;
int[][] dp = new int[n][aim + 1];
for (int j = 1; j <= aim; j++) {
dp[0][j] = max;
if (j - arr[0] >= 0 && dp[0][j - arr[0]] != max) {
dp[0][j] = dp[0][j - arr[0]] + 1;
}
}
int left = 0;
for (int i = 1; i < n; i++) {
for (int j = 1; j <= aim; j++) {
left = max;
if (j - arr[i] >= 0 && dp[i][j - arr[i]] != max) {
left = dp[i][j - arr[i]] + 1;
}
dp[i][j] = Math.min(left, dp[i - 1][j]);
}
}
return dp[n - 1][aim] != max ? dp[n - 1][aim] : -1;
}
public static int minCoins2(int[] arr, int aim) {
if (arr == null || arr.length == 0 || aim < 0) {
return -1;
}
int n = arr.length;
int max = Integer.MAX_VALUE;
int[] dp = new int[aim + 1];
for (int j = 1; j <= aim; j++) {
dp[j] = max;
if (j - arr[0] >= 0 && dp[j - arr[0]] != max) {
dp[j] = dp[j - arr[0]] + 1;
}
}
int left = 0;
for (int i = 1; i < n; i++) {
for (int j = 1; j <= aim; j++) {
left = max;
if (j - arr[i] >= 0 && dp[j - arr[i]] != max) {
left = dp[j - arr[i]] + 1;
}
dp[j] = Math.min(left, dp[j]);
}
}
return dp[aim] != max ? dp[aim] : -1;
}
public static int minCoins3(int[] arr, int aim) {
if (arr == null || arr.length == 0 || aim < 0) {
return -1;
}
int n = arr.length;
int max = Integer.MAX_VALUE;
int[][] dp = new int[n][aim + 1];
for (int j = 1; j <= aim; j++) {
dp[0][j] = max;
}
if (arr[0] <= aim) {
dp[0][arr[0]] = 1;
}
int leftup = 0; // 左上角某个位置的值
for (int i = 1; i < n; i++) {
for (int j = 1; j <= aim; j++) {
leftup = max;
if (j - arr[i] >= 0 && dp[i - 1][j - arr[i]] != max) {
leftup = dp[i - 1][j - arr[i]] + 1;
}
dp[i][j] = Math.min(leftup, dp[i - 1][j]);
}
}
return dp[n - 1][aim] != max ? dp[n - 1][aim] : -1;
}
public static int minCoins4(int[] arr, int aim) {
if (arr == null || arr.length == 0 || aim < 0) {
return -1;
}
int n = arr.length;
int max = Integer.MAX_VALUE;
int[] dp = new int[aim + 1];
for (int j = 1; j <= aim; j++) {
dp[j] = max;
}
if (arr[0] <= aim) {
dp[arr[0]] = 1;
}
int leftup = 0; // 左上角某个位置的值
for (int i = 1; i < n; i++) {
for (int j = aim; j > 0; j--) {
leftup = max;
if (j - arr[i] >= 0 && dp[j - arr[i]] != max) {
leftup = dp[j - arr[i]] + 1;
}
dp[j] = Math.min(leftup, dp[j]);
}
}
return dp[aim] != max ? dp[aim] : -1;
}
public static void main(String[] args) {
int[] arr1 = { 100, 20, 5, 10, 2, 50, 1 };
int aim1 = 17019;
System.out.println(minCoins1(arr1, aim1));
System.out.println(minCoins2(arr1, aim1));
int[] arr2 = { 10, 100, 2, 5, 5, 5, 10, 1, 1, 1, 2, 100 };
int aim2 = 223;
System.out.println(minCoins3(arr2, aim2));
System.out.println(minCoins4(arr2, aim2));
}
}
Problem_03_CoinsMin
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 常见的有棋盘覆盖和A+B问题,这类问题牵扯到的数值都比较大,如果用一般的数值类型,肯定输出不了,所以就要想一个办法...