15. Coin Change (Minimum Coins)
The problem can be found at the following link: Question Link
Problem Description
Given an array coins[] of distinct denominations and an integer sum, determine the minimum number of coins required to make up the given sum.
You have an infinite supply of each type of coin.
If the sum cannot be formed, return -1.
Examples
Example 1:
Input:
coins[] = [25, 10, 5]
sum = 30Output:
2Explanation:
The minimum number of coins required is 2 (25 + 5).
Example 2:
Input:
Output:
Explanation:
The minimum number of coins required is 3 (9 + 9 + 1).
Example 3:
Input:
Output:
Explanation:
For a target sum of 0, no coins are needed.
Example 4:
Input:
Output:
Explanation:
It's not possible to obtain a sum of 5 using the given coins.
Constraints:
$(1 \leq \text{sum} \times \text{coins.size()} \leq 10^6)$
$(0 \leq \text{sum} \leq 10^4)$
$(1 \leq \text{coins}[i] \leq 10^4)$
$(1 \leq \text{coins.size()} \leq 10^3)$
My Approach:
Optimized Dynamic Programming
Algorithm Steps:
Define
dp[j]as the minimum number of coins needed to obtainsum = j.Base Case:
dp[0] = 0(zero sum requires zero coins).Transition:
For each
coin, updatedp[j]for allj >= coinusing: $[ dp[j] = \min(dp[j], dp[j - coin] + 1) $]
Final Check: If
dp[sum] == ∞, return-1(sum cannot be formed).
Time and Auxiliary Space Complexity
Expected Time Complexity: O(N × sum), as we iterate over each coin and process all sums up to
sum.Expected Auxiliary Space Complexity: O(sum), as we maintain a 1D DP array of size
sum + 1.
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 minimum coins needed to make sumjusing the firsticoins.Base Case:
dp[i][0] = 0for alli(zero sum requires zero coins).dp[0][j] = ∞for allj > 0(zero coins can't form positive sum).
Recurrence Relation: $[ dp[i][j] = \min(dp[i-1][j], 1 + dp[i][j - coins[i-1]]) $]
Exclude the coin (
dp[i-1][j]).Include the coin (
dp[i][j - coins[i-1]] + 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
minCoins(index, sum)calculates the minimum coins needed using coins up toindex.Base Case:
If
sum == 0, return0(no coins needed).If
index < 0orsum < 0, return∞(not possible).
Recurrence Relation: $[ minCoins(index, sum) = \min(minCoins(index - 1, sum), 1 + minCoins(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