题目
给定不同面额的硬币 coins 和一个总金额 amount。编写一个函数来计算可以凑成总金额所需的最少的硬币个数。如果没有任何一种硬币组合能组成总金额,返回 -1。
示例 1:
输入: coins = [1, 2, 5], amount = 11
输出: 3
解释: 11 = 5 + 5 + 1
示例 2:
输入: coins = [2], amount = 3
输出: -1
思路 动态规划,自底向上
首先,如何去考虑动态规划,怎么将这个问题拆分为重复的子问题
然后,定义dp状态方程
dp[i] =min( dp[i - cins[j]] )+ 1
class Solution {
public int coinChange(int[] coins, int amount) {
int[] dp = new int[amount + 1];
int max = amount + 1; //dp数组初始化最大值
Arrays.fill(dp, max);
dp[0] = 0;
for (int i = 1; i <= amount; i++) {
for (int j = 0; j < coins.length; j++) { //循环硬币面额数组,找到最小值
if (coins[j] <= i) {
dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1);
}
}
}
return dp[amount] > amount ? -1 : dp[amount];
}
}
复杂度分析
时间复杂度:O(SN),总金额S,coins数组大小N
空间复杂度:O(S)
思路二 动态规划,自顶向下
这个类似于之前的斐波拉契系数一样,递归的思想是自顶向下,斐波拉契系数是将后面f(n-1)和f(n-2)相加,而这道题的思想是 对下面coins数组进行循环,找最小的数再加一
f(n) = min(f(n - coins[j] ))+ 1
class Solution {
int res = Integer.MAX_VALUE;
public int coinChange(int[] coins, int amount) {
if (coins.length == 0 || amount == 0) return -1;
dfs(coins, amount, 0);
return res == Integer.MAX_VALUE ? -1 : res;
}
public void dfs (int[] coins, int amount, int count) {
if (amount < 0) return;//凑不了amount金额,啥也不干,跳过
if (amount == 0) res = Math.min(res, count); //凑到了,选择最小的数量
for (int i = 0; i < coins.length; i++) {
dfs (coins, amount - coins[i], count + 1);
}
}
}
class Solution {
public int coinChange(int[] coins, int amount) {
if (coins.length == 0 ) return -1;
return dfs(coins, amount);
}
public int dfs (int[] coins, int amount) {
if (amount < 0) return -1;
if (amount == 0) return 0;
int min = Integer.MAX_VALUE;
for (int i = 0; i < coins.length; i++) {
int res = dfs (coins, amount - coins[i]);
if (res >= 0 && res < min) {
min = res + 1;
}
}
return min == Integer.MAX_VALUE ? -1 : min;
}
}
复杂度分析
时间复杂度:O(NS)
空间复杂度:O(S),最坏情况下,S的递归深度
优化,添加记忆数组
每一次求金额的最小情况,都有很多重复计算,使用一个额外数组进行记忆,以空间换时间
class Solution {
public int coinChange(int[] coins, int amount) {
if (coins.length == 0 ) return -1;
return dfs(coins, amount, new int[amount]);
}
public int dfs (int[] coins, int amount, int[] count) {
if (amount < 0) return -1;
if (amount == 0) return 0;
if (count[amount - 1] != 0) return count[amount - 1];
int min = Integer.MAX_VALUE;
for (int i = 0; i < coins.length; i++) {
int res = dfs (coins, amount - coins[i], count);
if (res >= 0 && res < min - 1) { //找到res最小的那个情况
min = res + 1;
}
}
count[amount -1] = (min == Integer.MAX_VALUE ? -1 : min);
return count[amount - 1];
}
}
复杂度分析
时间复杂度O(SN)
空间复杂度:O(S)
贪心加dfs
从面值小的 开始进行找最小值一般情况非常慢,我们从大面值硬币开始找
每一次都取最大硬币的 最大币数,这样一般情况会得到接近于解的值,但是不一定是解。我们可以通过这个解 对递归树进行疯狂剪枝
- 找最大值硬币的 最大数 带入进去,一步一步进行凑,如果凑不到金额,那么回溯 之前的大硬币的数量,进行减一。 这样类似于穷举的方法,但是 通过一开始得到的解可以排除大部分情况,除非奇葩情况。
int ans = Integer.MAX_VALUE;
public int coinChange(int[] coins, int amount) {
Arrays.sort(coins);
coinChange(coins.length-1, coins, 0, amount);
return ans == Integer.MAX_VALUE ? -1 : ans;
}
private void coinChange(int index, int[] coins, int count, int needAmount) {
//index进行最大面值硬币, count 当前这层的币数 needAmount 金额减去的余额
if (needAmount == 0) {
ans = Math.min(count, ans);
return;
}
if (index < 0) {
return;
}
int i = needAmount / coins[index];//最大面值硬币 的最大币数
for (int k = i; k >= 0 && count + k < ans; k--) { //count + k 进行剪枝
coinChange(index-1, coins, count+k, needAmount-k*coins[index]);
}
}
复杂度分析
时间复杂度O(Sn),但是 这个递归树进行疯狂剪枝
空间复杂度:O(N)