Coin Change Minimum Coins in DSA Javascript - Time & Space Complexity
We want to know how the time needed to find the minimum coins changes as the amount grows.
How does the number of coins and amount affect the work done?
Analyze the time complexity of the following code snippet.
function minCoins(coins, amount) {
const dp = Array(amount + 1).fill(Infinity);
dp[0] = 0;
for (let i = 1; i <= amount; i++) {
for (const coin of coins) {
if (coin <= i) {
dp[i] = Math.min(dp[i], dp[i - coin] + 1);
}
}
}
return dp[amount] === Infinity ? -1 : dp[amount];
}
This code finds the fewest coins needed to make a given amount using given coin values.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Nested loops: outer loop runs through amounts 1 to amount, inner loop runs through all coin types.
- How many times: Outer loop runs amount times, inner loop runs coins.length times for each outer iteration.
As the amount grows, the code checks each coin for every smaller amount up to the target.
| Input Size (amount) | Approx. Operations |
|---|---|
| 10 | 10 x number of coins |
| 100 | 100 x number of coins |
| 1000 | 1000 x number of coins |
Pattern observation: Operations grow roughly in a straight line with amount, multiplied by the number of coin types.
Time Complexity: O(amount x coins.length)
This means the time needed grows proportionally with the amount and the number of coin types.
[X] Wrong: "The time depends only on the amount, not on the number of coins."
[OK] Correct: Each coin is checked for every amount, so more coin types mean more checks and more time.
Understanding this helps you explain how dynamic programming solves problems efficiently by building up answers step-by-step.
"What if we used recursion with memoization instead of loops? How would the time complexity change?"
Practice
Given coins of different denominations and a total amount, find the minimum number of coins needed to make that amount.Solution
Step 1: Understand the problem goal
The problem asks for the fewest coins needed to reach a given amount using available coin denominations.Step 2: Identify the correct interpretation
Among the options, only the minimum number of coins needed matches the problem description.Final Answer:
Minimum number of coins needed to make the amount -> Option AQuick Check:
Minimum coins = problem goal [OK]
- Confusing minimum with maximum coins
- Thinking about total sum of coins instead of count
- Ignoring the amount to be formed
const dp = new Array(amount + 1).fill(____);Solution
Step 1: Understand DP array initialization
We initialize dp array with Infinity to represent that initially no amount can be formed except 0.Step 2: Confirm correct fill value
Filling with Infinity helps us find minimum coins by comparing and updating values.Final Answer:
Infinity -> Option DQuick Check:
DP array filled with Infinity for min comparisons [OK]
- Using 0 which implies amount can be formed with zero coins
- Using -1 which is not suitable for min comparisons
- Using null which causes errors in calculations
const coins = [1, 2, 5];
const amount = 11;
function minCoins(coins, amount) {
const dp = new Array(amount + 1).fill(Infinity);
dp[0] = 0;
for (let i = 1; i <= amount; i++) {
for (const coin of coins) {
if (coin <= i) {
dp[i] = Math.min(dp[i], dp[i - coin] + 1);
}
}
}
return dp[amount] === Infinity ? -1 : dp[amount];
}
console.log(minCoins(coins, amount));Solution
Step 1: Trace DP array updates for amount 11
Using coins 1, 2, 5, minimum coins for 11 are calculated as 3 (5+5+1) or 4 (5+2+2+2). The code finds 3 coins.Step 2: Verify minimum coins count
Actually, 11 = 5 + 5 + 1 uses 3 coins, which is minimum.Final Answer:
3 -> Option CQuick Check:
11 = 5+5+1 = 3 coins [OK]
- Counting coins incorrectly (e.g., 5 coins instead of 3)
- Returning -1 when solution exists
- Ignoring smaller coins in calculation
function minCoins(coins, amount) {
const dp = new Array(amount + 1).fill(Infinity);
dp[0] = 0;
for (let i = 0; i <= amount; i++) {
for (const coin of coins) {
if (coin <= i) {
dp[i] = Math.min(dp[i], dp[i - coin] + 1);
}
}
}
return dp[amount] === Infinity ? -1 : dp[amount];
}Solution
Step 1: Analyze loop start index
The outer loop starts at i = 0, but dp[0] is base case and should not be updated. Starting at 1 avoids unnecessary updates.Step 2: Confirm correct loop range
Starting from i = 1 ensures dp values build correctly from base case.Final Answer:
Loop should start from i = 1, not 0 -> Option AQuick Check:
Start loop at 1 to build dp correctly [OK]
- Starting loop at 0 causing incorrect dp updates
- Filling dp with 0 causing wrong min calculations
- Wrong return index causing wrong output
Use the Coin Change Minimum Coins approach.
Solution
Step 1: Try combinations to form 13
Using coins 7, 3, and 2, one way is 7 + 3 + 3 = 13 with 3 coins.Step 2: Check if fewer coins possible
No combination uses fewer than 4 coins to make 13 with these denominations.Final Answer:
4 -> Option BQuick Check:
13 can be formed with 4 coins minimum [OK]
- Assuming no solution (-1) without checking combinations
- Counting coins incorrectly
- Ignoring coin reuse possibilities
Ready to prove it?
Pass the test and this pattern moves unknown
