Coin Change Minimum Coins in DSA C# - Time & Space Complexity
We want to know how the time needed to find the minimum coins changes as we increase the amount or the coin types.
How does the algorithm's work grow when the input gets bigger?
Analyze the time complexity of the following code snippet.
int MinCoins(int[] coins, int amount) {
int[] dp = new int[amount + 1];
Array.Fill(dp, amount + 1);
dp[0] = 0;
for (int i = 1; i <= amount; i++) {
foreach (int coin in coins) {
if (coin <= i) {
dp[i] = Math.Min(dp[i], 1 + dp[i - coin]);
}
}
}
return dp[amount] > amount ? -1 : dp[amount];
}
This code finds the minimum number of coins needed to make a given amount using dynamic programming.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Nested loops - outer loop runs over amounts from 1 to amount, inner loop runs over 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 outer loop runs more times. For each amount, we check all coins.
| 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: The work grows linearly with the amount and linearly with the number of coin types.
Time Complexity: O(amount x number_of_coins)
This means the time needed grows proportionally with the amount and the number of coin types.
[X] Wrong: "The algorithm runs in constant time because it just loops once."
[OK] Correct: The algorithm has two loops nested: one over the amount and one over the coins, so time grows with both.
Understanding this time complexity helps you explain how your solution scales and why dynamic programming is efficient here.
"What if we sorted the coins and stopped checking when coin > current amount? How would the time complexity change?"
Practice
Given coins and an amount, find the minimum number of coins needed to make that amount.Solution
Step 1: Understand the problem goal
The problem asks for the minimum number of coins needed to make a given amount using available coin denominations.Step 2: Clarify what is asked
We want the smallest count of coins that sum exactly to the amount, not total value or max coins.Final Answer:
The smallest number of coins needed to make the amount -> Option AQuick Check:
Minimum coins = smallest count [OK]
- Confusing minimum coins with total coin value
- Thinking about maximum coins instead of minimum
- Counting coin types instead of coin quantity
Solution
Step 1: Check array size and initialization
We need an array of size 6 for amounts 0 to 5. Amount 0 requires 0 coins, others start with a large number.Step 2: Verify each option
int[] minCoins = new int[6] {0, int.MaxValue, int.MaxValue, int.MaxValue, int.MaxValue, int.MaxValue}; correctly initializes size 6 with 0 at index 0 and int.MaxValue for others. Others either have wrong size or wrong initialization.Final Answer:
int[] minCoins = new int[6] {0, int.MaxValue, int.MaxValue, int.MaxValue, int.MaxValue, int.MaxValue}; -> Option BQuick Check:
Array size 6, 0 at start, max values after [OK]
- Using wrong array size
- Setting 0 coins for all amounts
- Setting int.MaxValue for amount 0
int[] coins = {1, 3, 4};
int amount = 6;
int[] minCoins = new int[amount + 1];
for (int i = 1; i <= amount; i++) minCoins[i] = int.MaxValue;
for (int i = 1; i <= amount; i++) {
foreach (int coin in coins) {
if (coin <= i && minCoins[i - coin] != int.MaxValue) {
minCoins[i] = Math.Min(minCoins[i], minCoins[i - coin] + 1);
}
}
}
Console.WriteLine(minCoins[amount]);Solution
Step 1: Initialize minCoins array
minCoins[0] = 0 (default), others set to int.MaxValue initially.Step 2: Calculate minCoins for amounts 1 to 6
For amount 6, check coins 1,3,4:
- Using coin 1: minCoins[5] + 1
- Using coin 3: minCoins[3] + 1
- Using coin 4: minCoins[2] + 1
Calculations:
minCoins[1] = 1 (coin 1)
minCoins[2] = 2 (1+1)
minCoins[3] = 1 (coin 3)
minCoins[4] = 1 (coin 4)
minCoins[5] = 2 (3+1 or 4+1)
minCoins[6] = min(minCoins[5]+1, minCoins[3]+1, minCoins[2]+1) = min(3, 2, 3) = 2Final Answer:
2 -> Option DQuick Check:
Minimum coins for 6 = 2 (3+3 or 4+1+1) [OK]
- Not updating minCoins correctly
- Ignoring int.MaxValue checks
- Miscounting coin combinations
int[] coins = {1, 2, 5};
int amount = 11;
int[] minCoins = new int[amount + 1];
for (int i = 1; i <= amount; i++) minCoins[i] = int.MaxValue;
for (int i = 1; i <= amount; i++) {
foreach (int coin in coins) {
if (coin < i && minCoins[i - coin] != int.MaxValue) {
minCoins[i] = Math.Min(minCoins[i], minCoins[i - coin] + 1);
}
}
}
Console.WriteLine(minCoins[amount]);Solution
Step 1: Analyze the if condition
The condition 'coin < i' excludes cases where coin equals i, missing direct matches.Step 2: Correct the condition
Changing to 'coin <= i' allows using coins that exactly match the current amount, fixing the logic.Final Answer:
Change 'coin < i' to 'coin <= i' in the if condition -> Option CQuick Check:
Include coin equal to amount for correct min coins [OK]
- Using < instead of <= excludes valid coins
- Initializing minCoins incorrectly
- Removing necessary condition checks
Solution
Step 1: Proper initialization
Set minCoins[0] = 0 because zero coins are needed for amount 0; others set to int.MaxValue to represent unreachable amounts initially.Step 2: Update minCoins using dynamic programming
For each amount i, check each coin less than or equal to i. If minCoins[i - coin] is reachable, update minCoins[i] to the minimum of current value and minCoins[i - coin] + 1.Final Answer:
Initialize minCoins with 0 for amount 0 and int.MaxValue for others; update minCoins[i] by checking coins <= i and minCoins[i - coin] + 1 -> Option AQuick Check:
DP with correct initialization and update finds minimum coins [OK]
- Initializing all minCoins to zero
- Using recursion without memoization causing inefficiency
- Incorrectly updating minCoins by subtraction
Ready to prove it?
Pass the test and this pattern moves unknown
