comments | difficulty | edit_url | tags | ||
---|---|---|---|---|---|
true |
中等 |
|
给你一个整数数组 coins
表示不同面额的硬币,另给一个整数 amount
表示总金额。
请你计算并返回可以凑成总金额的硬币组合数。如果任何硬币组合都无法凑出总金额,返回 0
。
假设每一种面额的硬币有无限个。
题目数据保证结果符合 32 位带符号整数。
示例 1:
输入:amount = 5, coins = [1, 2, 5] 输出:4 解释:有四种方式可以凑成总金额: 5=5 5=2+2+1 5=2+1+1+1 5=1+1+1+1+1
示例 2:
输入:amount = 3, coins = [2] 输出:0 解释:只用面额 2 的硬币不能凑成总金额 3 。
示例 3:
输入:amount = 10, coins = [10] 输出:1
提示:
1 <= coins.length <= 300
1 <= coins[i] <= 5000
coins
中的所有值 互不相同0 <= amount <= 5000
我们定义
我们可以枚举使用的最后一枚硬币的数量
其中
不妨令
将式子二代入式子一,得到:
最终的答案为
时间复杂度
class Solution:
def change(self, amount: int, coins: List[int]) -> int:
m, n = len(coins), amount
f = [[0] * (n + 1) for _ in range(m + 1)]
f[0][0] = 1
for i, x in enumerate(coins, 1):
for j in range(n + 1):
f[i][j] = f[i - 1][j]
if j >= x:
f[i][j] += f[i][j - x]
return f[m][n]
class Solution {
public int change(int amount, int[] coins) {
int m = coins.length, n = amount;
int[][] f = new int[m + 1][n + 1];
f[0][0] = 1;
for (int i = 1; i <= m; ++i) {
for (int j = 0; j <= n; ++j) {
f[i][j] = f[i - 1][j];
if (j >= coins[i - 1]) {
f[i][j] += f[i][j - coins[i - 1]];
}
}
}
return f[m][n];
}
}
class Solution {
public:
int change(int amount, vector<int>& coins) {
int m = coins.size(), n = amount;
unsigned f[m + 1][n + 1];
memset(f, 0, sizeof(f));
f[0][0] = 1;
for (int i = 1; i <= m; ++i) {
for (int j = 0; j <= n; ++j) {
f[i][j] = f[i - 1][j];
if (j >= coins[i - 1]) {
f[i][j] += f[i][j - coins[i - 1]];
}
}
}
return f[m][n];
}
};
func change(amount int, coins []int) int {
m, n := len(coins), amount
f := make([][]int, m+1)
for i := range f {
f[i] = make([]int, n+1)
}
f[0][0] = 1
for i := 1; i <= m; i++ {
for j := 0; j <= n; j++ {
f[i][j] = f[i-1][j]
if j >= coins[i-1] {
f[i][j] += f[i][j-coins[i-1]]
}
}
}
return f[m][n]
}
function change(amount: number, coins: number[]): number {
const [m, n] = [coins.length, amount];
const f: number[][] = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
f[0][0] = 1;
for (let i = 1; i <= m; ++i) {
for (let j = 0; j <= n; ++j) {
f[i][j] = f[i - 1][j];
if (j >= coins[i - 1]) {
f[i][j] += f[i][j - coins[i - 1]];
}
}
}
return f[m][n];
}
我们注意到
class Solution:
def change(self, amount: int, coins: List[int]) -> int:
n = amount
f = [1] + [0] * n
for x in coins:
for j in range(x, n + 1):
f[j] += f[j - x]
return f[n]
class Solution {
public int change(int amount, int[] coins) {
int n = amount;
int[] f = new int[n + 1];
f[0] = 1;
for (int x : coins) {
for (int j = x; j <= n; ++j) {
f[j] += f[j - x];
}
}
return f[n];
}
}
class Solution {
public:
int change(int amount, vector<int>& coins) {
int n = amount;
unsigned f[n + 1];
memset(f, 0, sizeof(f));
f[0] = 1;
for (int x : coins) {
for (int j = x; j <= n; ++j) {
f[j] += f[j - x];
}
}
return f[n];
}
};
func change(amount int, coins []int) int {
n := amount
f := make([]int, n+1)
f[0] = 1
for _, x := range coins {
for j := x; j <= n; j++ {
f[j] += f[j-x]
}
}
return f[n]
}
function change(amount: number, coins: number[]): number {
const n = amount;
const f: number[] = Array(n + 1).fill(0);
f[0] = 1;
for (const x of coins) {
for (let j = x; j <= n; ++j) {
f[j] += f[j - x];
}
}
return f[n];
}