硬币。给定数量不限的硬币,币值为25分、10分、5分和1分,编写代码计算n分有几种表示法。(结果可能会很大,你需要将结果模上1000000007)
示例1:
输入: n = 5 输出:2 解释: 有两种方式可以凑成总金额: 5=5 5=1+1+1+1+1
示例2:
输入: n = 10 输出:4 解释: 有四种方式可以凑成总金额: 10=10 10=5+5 10=5+1+1+1+1+1 10=1+1+1+1+1+1+1+1+1+1
说明:
注意:
你可以假设:
- 0 <= n (总金额) <= 1000000
我们定义
考虑
不妨令
将二式代入一式,得到:
最后的答案即为
时间复杂度
我们注意到,$f[i][j]$ 的计算只与
class Solution:
def waysToChange(self, n: int) -> int:
mod = 10**9 + 7
coins = [25, 10, 5, 1]
f = [[0] * (n + 1) for _ in range(5)]
f[0][0] = 1
for i, c in enumerate(coins, 1):
for j in range(n + 1):
f[i][j] = f[i - 1][j]
if j >= c:
f[i][j] = (f[i][j] + f[i][j - c]) % mod
return f[-1][n]
class Solution {
public int waysToChange(int n) {
final int mod = (int) 1e9 + 7;
int[] coins = {25, 10, 5, 1};
int[][] f = new int[5][n + 1];
f[0][0] = 1;
for (int i = 1; i <= 4; ++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] + f[i][j - coins[i - 1]]) % mod;
}
}
}
return f[4][n];
}
}
class Solution {
public:
int waysToChange(int n) {
const int mod = 1e9 + 7;
vector<int> coins = {25, 10, 5, 1};
int f[5][n + 1];
memset(f, 0, sizeof(f));
f[0][0] = 1;
for (int i = 1; i <= 4; ++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] + f[i][j - coins[i - 1]]) % mod;
}
}
}
return f[4][n];
}
};
func waysToChange(n int) int {
const mod int = 1e9 + 7
coins := []int{25, 10, 5, 1}
f := make([][]int, 5)
for i := range f {
f[i] = make([]int, n+1)
}
f[0][0] = 1
for i := 1; i <= 4; 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] + f[i][j-coins[i-1]]) % mod
}
}
}
return f[4][n]
}
function waysToChange(n: number): number {
const mod = 10 ** 9 + 7;
const coins: number[] = [25, 10, 5, 1];
const f: number[][] = Array(5)
.fill(0)
.map(() => Array(n + 1).fill(0));
f[0][0] = 1;
for (let i = 1; i <= 4; ++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] + f[i][j - coins[i - 1]]) % mod;
}
}
}
return f[4][n];
}
class Solution:
def waysToChange(self, n: int) -> int:
mod = 10**9 + 7
coins = [25, 10, 5, 1]
f = [1] + [0] * n
for c in coins:
for j in range(c, n + 1):
f[j] = (f[j] + f[j - c]) % mod
return f[n]
class Solution {
public int waysToChange(int n) {
final int mod = (int) 1e9 + 7;
int[] coins = {25, 10, 5, 1};
int[] f = new int[n + 1];
f[0] = 1;
for (int c : coins) {
for (int j = c; j <= n; ++j) {
f[j] = (f[j] + f[j - c]) % mod;
}
}
return f[n];
}
}
class Solution {
public:
int waysToChange(int n) {
const int mod = 1e9 + 7;
vector<int> coins = {25, 10, 5, 1};
int f[n + 1];
memset(f, 0, sizeof(f));
f[0] = 1;
for (int c : coins) {
for (int j = c; j <= n; ++j) {
f[j] = (f[j] + f[j - c]) % mod;
}
}
return f[n];
}
};
func waysToChange(n int) int {
const mod int = 1e9 + 7
coins := []int{25, 10, 5, 1}
f := make([]int, n+1)
f[0] = 1
for _, c := range coins {
for j := c; j <= n; j++ {
f[j] = (f[j] + f[j-c]) % mod
}
}
return f[n]
}
function waysToChange(n: number): number {
const mod = 10 ** 9 + 7;
const coins: number[] = [25, 10, 5, 1];
const f: number[] = new Array(n + 1).fill(0);
f[0] = 1;
for (const c of coins) {
for (let i = c; i <= n; ++i) {
f[i] = (f[i] + f[i - c]) % mod;
}
}
return f[n];
}