-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path518.Change.cs
103 lines (79 loc) · 2.94 KB
/
518.Change.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// 518. Coin Change II
// You are given an integer array coins representing coins of different denominations and an integer amount representing a total
// amount of money.
// 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.
// You may assume that you have an infinite number of each kind of coin.
// The answer is guaranteed to fit into a signed 32-bit integer.
// Example 1:
// Input: amount = 5, coins = [1,2,5]
// Output: 4
// Explanation: there are four ways to make up the amount:
// 5=5
// 5=2+2+1
// 5=2+1+1+1
// 5=1+1+1+1+1
// Example 2:
// Input: amount = 3, coins = [2]
// Output: 0
// Explanation: the amount of 3 cannot be made up just with coins of 2.
// Example 3:
// Input: amount = 10, coins = [10]
// Output: 1
// Constraints:
// 1 <= coins.length <= 300
// 1 <= coins[i] <= 5000
// All the values of coins are unique.
// 0 <= amount <= 5000
public class Solution {
public int Change(int amount, int[] coins) {
return Combination(amount, coins, 0);
}
public int Combination(int amount, int[] coins, int index) {
if (amount == 0)
return 1;
if (index == coins.Length || amount < 0)
return 0;
// Include current coin or skip to the next one
return Combination(amount - coins[index], coins, index)
+ Combination(amount, coins, index + 1);
}
}
//O(amount * n) time complexity and efficiently avoids recomputation.
public class Solution {
private int[,] memo;
public int Change(int amount, int[] coins) {
memo = new int[amount + 1, coins.Length + 1];
for (int i = 0; i <= amount; i++)
for (int j = 0; j <= coins.Length; j++)
memo[i, j] = -1;
return Combination(amount, coins, 0);
}
public int Combination(int amount, int[] coins, int index) {
if (amount == 0) return 1;
if (index == coins.Length || amount < 0) return 0;
if (memo[amount, index] != -1) return memo[amount, index];
memo[amount, index] = Combination(amount - coins[index], coins, index)
+ Combination(amount, coins, index + 1);
return memo[amount, index];
}
}
//Time: O(amount×coins.Length)
//Space: O(amount)
public class Solution {
public int Change(int amount, int[] coins) {
int[] dp = new int[amount + 1];
dp[0] = 1; // There's one way to make amount 0 — by using no coins
foreach (var coin in coins) {
for (int i = coin; i <= amount; i++) {
dp[i] += dp[i - coin];
}
}
return dp[amount];
}
}
// Base Case:
// dp[0] = 1 because there's exactly one way to make amount 0, which is by choosing no coins.
// DP Array Update:
// For each coin, iterate from coin to amount and accumulate possible combinations.
// dp[i] += dp[i - coin] counts how many ways we can form i by using coin.