Skip to content

Commit cb0f876

Browse files
committed
leetcode
1 parent b67373a commit cb0f876

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

CoinChangeII/coin_change_ii.dart

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
3+
-* 518. Coin Change II *-
4+
5+
You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.
6+
7+
Return the number of combinations that make up that amount. If that amount of money cannot be made up by any combination of the coins, return 0.
8+
9+
You may assume that you have an infinite number of each kind of coin.
10+
11+
The answer is guaranteed to fit into a signed 32-bit integer.
12+
13+
14+
15+
Example 1:
16+
17+
Input: amount = 5, coins = [1,2,5]
18+
Output: 4
19+
Explanation: there are four ways to make up the amount:
20+
5=5
21+
5=2+2+1
22+
5=2+1+1+1
23+
5=1+1+1+1+1
24+
Example 2:
25+
26+
Input: amount = 3, coins = [2]
27+
Output: 0
28+
Explanation: the amount of 3 cannot be made up just with coins of 2.
29+
Example 3:
30+
31+
Input: amount = 10, coins = [10]
32+
Output: 1
33+
34+
35+
Constraints:
36+
37+
1 <= coins.length <= 300
38+
1 <= coins[i] <= 5000
39+
All the values of coins are unique.
40+
0 <= amount <= 5000
41+
42+
*/
43+
44+
// Tabulation
45+
class A {
46+
int change(int amount, List<int> coins) {
47+
final int m = coins.length;
48+
final int n = amount;
49+
final List<List<int>> dp =
50+
List.filled(m + 1, 0).map((e) => List.filled(n + 1, 0)).toList();
51+
// int dp[m+1][n+1];
52+
for (int i = 0; i < m + 1; i++) {
53+
for (int j = 0; j < n + 1; j++) {
54+
if (j == 0) {
55+
dp[i][j] = 1;
56+
}
57+
if (i == 0) {
58+
dp[i][j] = 0;
59+
}
60+
}
61+
}
62+
dp[0][0] = 1;
63+
for (int i = 1; i < m + 1; i++) {
64+
for (int j = 1; j < n + 1; j++) {
65+
if (coins[i - 1] <= j) {
66+
dp[i][j] = dp[i][j - coins[i - 1]] + dp[i - 1][j];
67+
} else
68+
dp[i][j] = dp[i - 1][j];
69+
}
70+
}
71+
return dp[m][n];
72+
}
73+
}
74+
75+
// Dynamic Programming
76+
class Solution {
77+
final List<List<int>> dp =
78+
List.generate(5001, (_) => List<int>.filled(301, -1));
79+
int solve(int amount, List<int> coins, int i) {
80+
if (amount == 0) return 1;
81+
82+
if (dp[amount][i] != -1) return dp[amount][i];
83+
84+
if (i == coins.length) return 0;
85+
86+
int tk = 0;
87+
if (coins[i] <= amount) {
88+
tk = solve(amount - coins[i], coins, i);
89+
}
90+
final int nt = solve(amount, coins, i + 1);
91+
return dp[amount][i] = tk + nt;
92+
}
93+
94+
int change(int amount, List<int> coins) {
95+
for (int i = 0; i < 5001; i++) {
96+
dp[i] = List<int>.filled(301, -1);
97+
}
98+
return solve(amount, coins, 0);
99+
}
100+
}
101+
102+
// Tabulation with Space Optimization
103+
class C {
104+
int change(int amount, List<int> coins) {
105+
final List<int> dp = List<int>.filled(amount + 1, 0);
106+
dp[0] = 1;
107+
for (int i = 0; i < coins.length; i++) {
108+
for (int j = coins[i]; j <= amount; j++) {
109+
dp[j] += dp[j - coins[i]];
110+
}
111+
}
112+
return dp[amount];
113+
}
114+
}

CoinChangeII/coin_change_ii.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package main
2+
3+
func change(amount int, coins []int) int {
4+
dp := make([]int, amount+1)
5+
dp[0] = 1
6+
7+
for i := 0; i < len(coins); i++ {
8+
for j := coins[i]; j <= amount; j++ {
9+
dp[j] += dp[j-coins[i]]
10+
}
11+
}
12+
13+
return dp[amount]
14+
}

CoinChangeII/coin_change_ii.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Coin Change
2+
3+
## [GO](https://leetcode.com/problems/coin-change-ii/solutions/3893332/go-dynamic-simple-fast-and-easy-with-explanation/)
4+
5+
## [DART](https://leetcode.com/problems/coin-change-ii/solutions/3893321/dynamic-programming-simple-fast-and-easy-with-explanation/)

0 commit comments

Comments
 (0)