14. Coin Change (Count Ways)
The problem can be found at the following link: Question Link
Problem Description
Given an integer array coins[] representing different denominations of currency and an integer sum, find the number of ways to make sum using any number of coins.
🔹 Note: You have an infinite supply of each type of coin.
Examples
Example 1:
Input:
coins = [1, 2, 3], sum = 4Output:
4Explanation:
There are 4 ways to make 4 using given coins:
[1, 1, 1, 1][1, 1, 2][2, 2][1, 3]
Example 2:
Input:
Output:
Explanation:
There are 5 ways to make 10:
[2, 2, 2, 2, 2][2, 2, 3, 3][2, 2, 6][2, 3, 5][5, 5]
Constraints:
$1 \leq \text{Number of Coins} \leq 10^3$
$1 \leq \text{sum} \leq 10^6$
$1 \leq \text{coins}[i] \leq 10^3$
My Approach:
Optimized Dynamic Programming
Algorithm Steps:
Use a 1D DP array
dp[], wheredp[i]stores the number of ways to make sumi.Base Case:
dp[0] = 1(There is one way to make sum0: choose nothing).
Transition:
For each
coin, update all sums fromcointosum.dp[j] += dp[j - coin](Include current coin).
Time and Auxiliary Space Complexity
Expected Time Complexity:
O(N × sum), whereNis the number of coins.Expected Auxiliary Space Complexity:
O(sum), as we only store a 1D DP array.
Code (C++)
⚡ Alternative Approaches
2️⃣ Dynamic Programming (O(N×sum) Time, O(N×sum) Space) — 2D DP
Algorithm Steps:
Use a 2D DP table where
dp[i][j]represents the number of ways to make sumjusing the firsticoins.Base Case:
dp[0][0] = 1(one way to make sum0with zero coins).dp[i][0] = 1for alli(only one way to make sum0: choose nothing).
Recurrence Relation: $[ \text{dp}[i][j] = \text{dp}[i-1][j] + \text{dp}[i][j - \text{coins}[i-1]] $]
Exclude the coin (
dp[i-1][j]).Include the coin (
dp[i][j - coins[i-1]]).
✅ Time Complexity: O(N × sum)
✅ Space Complexity: O(N × sum)
3️⃣ Recursive + Memoization (O(N×sum) Time, O(N×sum) Space)
Algorithm Steps:
Recursive function
countWays(index, sum)calculates the number of ways using coins up toindex.Base Case:
If
sum == 0, return1(valid way found).If
index < 0orsum < 0, return0(invalid case).
Recurrence Relation: $[ \text{countWays(index, sum)} = \text{countWays(index - 1, sum)} + \text{countWays(index, sum - coins[index])} $]
Exclude the current coin.
Include the current coin.
Use memoization (
dp[index][sum]) to avoid redundant calculations.
✅ Time Complexity: O(N × sum)
✅ Space Complexity: O(N × sum)
Comparison of Approaches
Approach
⏱️ Time Complexity
🗂️ Space Complexity
✅ Pros
⚠️ Cons
1D Space Optimized DP
🟡 O(N × sum)
🟢 O(sum)
Most efficient space-wise
Requires careful indexing
2D DP (Tabulation)
🟡 O(N × sum)
🔴 O(N × sum)
Easy to implement, intuitive
High space usage
Recursive + Memoization
🟡 O(N × sum)
🔴 O(N × sum)
Natural recursion flow
Stack overhead
✅ Best Choice?
If optimizing space: Use 1D DP (Space-Optimized).
If space is not a concern: Use 2D DP (Tabulation) for easy understanding.
For recursion lovers: Use Recursive + Memoization.
Code (Java)
Code (Python)
Contribution and Support:
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Let’s make this learning journey more collaborative!
⭐ If you find this helpful, please give this repository a star! ⭐
📍Visitor Count
Last updated