Coin Change Minimum Coins in DSA Typescript - Time & Space Complexity
We want to understand how the time needed to find the minimum coins changes as the amount grows.
How does the number of steps grow when we try to make bigger amounts with given coins?
Analyze the time complexity of the following code snippet.
function minCoins(coins: number[], amount: number): number {
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];
}
This code finds the fewest coins needed to make a given amount using the coins available.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Nested loops where for each amount from 1 to the target, we check all coins.
- How many times: Outer loop runs
amounttimes; inner loop runscoins.lengthtimes for each outer iteration.
As the amount grows, the steps grow by roughly the amount times the number of coins.
| Input Size (amount) | Approx. Operations |
|---|---|
| 10 | 10 x coins.length |
| 100 | 100 x coins.length |
| 1000 | 1000 x coins.length |
Pattern observation: The work grows linearly with the amount and linearly with the number of coins.
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 coins mean more checks and more time.
Understanding this complexity helps you explain how your solution scales and shows you can analyze nested loops clearly.
"What if we used recursion with memoization instead of loops? How would the time complexity change?"
Practice
Solution
Step 1: Understand the problem goal
The problem asks for the minimum number of coins needed to make a specific amount.Step 2: Differentiate from other coin problems
It is not about counting coins or maximum amount, but minimizing coin count for exact amount.Final Answer:
The least number of coins needed to make a given amount -> Option CQuick Check:
Minimum coins = least number needed [OK]
- Confusing minimum coins with total coin types
- Thinking problem asks for maximum amount
- Ignoring the exact amount requirement
Solution
Step 1: Check parameter types and order
The function should take an array of coin values (number[]) and an amount (number).Step 2: Check return type
The function returns a number representing minimum coins needed.Final Answer:
function minCoins(coins: number[], amount: number): number -> Option AQuick Check:
Correct types and return number [OK]
- Swapping parameter order
- Using wrong types like string instead of number
- Returning void or array instead of number
function minCoins(coins: number[], amount: number): number {
const dp = Array(amount + 1).fill(amount + 1);
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] > amount ? -1 : dp[amount];
}
console.log(minCoins([1, 2, 5], 11));Solution
Step 1: Understand dp array initialization
dp array size is 12 (0 to 11), initialized with 12 (amount+1), dp[0] = 0.Step 2: Calculate dp values for amount 11
Using coins 1,2,5, minimum coins for 11 is 3 (5+5+1).Final Answer:
3 -> Option BQuick Check:
Minimum coins for 11 with [1,2,5] = 3 [OK]
- Returning dp[amount] without checking if > amount
- Misunderstanding dp initialization
- Confusing coin values and indices
function minCoins(coins: number[], amount: number): number {
const dp = new Array(amount + 1).fill(0);
dp[0] = 0;
for (let i = 1; i <= amount; i++) {
dp[i] = Infinity;
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: Check coin usage condition
The condition 'coin < i' skips cases where coin equals i, missing exact matches.Step 2: Correct condition to include equal coins
Change condition to 'coin <= i' to allow using coin equal to current amount.Final Answer:
The condition should be coin <= i, not coin < i -> Option AQuick Check:
Include coin equal to amount [OK]
- Using < instead of <= in coin check
- Initializing dp array incorrectly
- Wrong return condition check
Solution
Step 1: Try combinations to form 13
Possible combos: 7+3+3 = 3 coins, 2+2+2+7 = 4 coins, etc.Step 2: Identify minimum coins
Minimum coins is 3 using 7, 3, and 3.Final Answer:
3 -> Option DQuick Check:
7+3+3 = 13 with 3 coins [OK]
- Assuming no solution (-1) without checking combos
- Counting coins incorrectly
- Ignoring combinations with repeated coins
Ready to prove it?
Pass the test and this pattern moves unknown
