11.Coin Change
The problem can be found at the following link: Question Link
My Approach
To solve this problem, I use dynamic programming. I create a 2D DP array dp
where dp[i][s]
represents the number of ways to make the sum s
using the first i
coins. I initialize dp[0][s]
with 1
if s
is divisible by the value of the first coin as base case.
Then, I iterate through the coins and the target sum. For each combination, I calculate two possibilities:
Not taking the current coin (
nottake = dp[i - 1][s]
)Taking the current coin (
take = dp[i][s - coins[i]]
ifs - coins[i] >= 0
)
The total number of ways to make the sum s
using the first i
coins is dp[i][s] = take + nottake
.
Explanation with Example
Consider the example coins = [1, 2, 3]
, N = 3
, and sum = 4
. The DP table would look like this:
0
1
1
1
1
1
1
1
1
2
2
3
2
1
1
2
3
4
The value at dp[2][4]
represents the number of ways to make the sum 4 using coins [1, 2, 3]
, which is 4.
Time and Auxiliary Space Complexity
Time Complexity:
O(N * sum)
, whereN
is the number of coins andsum
is the given target sum.Auxiliary Space Complexity:
O(N * sum)
, as we use a 2D DP array to store intermediate results.
Code (C++)
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the getlost01/gfg-potd repository.
Last updated