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 the amount grows.
How does the number of coins and the amount affect the work done?
Analyze the time complexity of the following code snippet.
int minCoins(int coins[], int n, int amount) {
int dp[amount + 1];
dp[0] = 0;
for (int i = 1; i <= amount; i++) {
dp[i] = INT_MAX;
for (int j = 0; j < n; j++) {
if (coins[j] <= i && dp[i - coins[j]] != INT_MAX) {
dp[i] = dp[i] < dp[i - coins[j]] + 1 ? dp[i] : dp[i - coins[j]] + 1;
}
}
}
return dp[amount];
}
This code finds the minimum number of coins needed to make the given amount using the coin denominations.
Look at the loops that repeat work:
- Primary operation: The inner loop checks all coin types for each amount value.
- How many times: The outer loop runs once for each amount from 1 to amount (amount times), and the inner loop runs for each coin type (n times).
As the amount grows, the work grows by checking all coins for each amount value.
| Input Size (amount) | Approx. Operations |
|---|---|
| 10 | 10 * number_of_coins |
| 100 | 100 * number_of_coins |
| 1000 | 1000 * number_of_coins |
Pattern observation: The operations grow roughly in a straight line with the amount, multiplied by the number of coin types.
Time Complexity: O(amount * n)
This means the time grows proportionally to the amount times the number of coin types.
[X] Wrong: "The time depends only on the amount, not the number of coins."
[OK] Correct: Each coin type is checked for every amount, so more coin types mean more work.
Understanding this helps you explain how dynamic programming solves problems efficiently by breaking them into smaller parts.
"What if we used recursion without memoization? 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 using given coin denominations.Step 2: Identify the correct description
The smallest number of coins needed to make a given amount correctly describes this goal, while others describe unrelated concepts.Final Answer:
The smallest number of coins needed to make a given amount -> Option AQuick Check:
Minimum coins = smallest count needed [OK]
- Confusing total coin count with minimum coins
- Thinking about coin values instead of counts
- Mixing up largest coin with minimum coins
Solution
Step 1: Understand DP initialization
We initialize dp array with a large number (like INT_MAX) to represent impossible states initially.Step 2: Identify correct initialization
int dp[amount+1]; for(int i=0; i<=amount; i++) dp[i] = INT_MAX; correctly sets all dp values to INT_MAX, except dp[0] which is usually set to 0 later.Final Answer:
int dp[amount+1]; for(int i=0; i<=amount; i++) dp[i] = INT_MAX; -> Option DQuick Check:
Initialize dp with max values for minimum search [OK]
- Initializing dp with zeros causes wrong minimum calculations
- Using -1 without handling leads to errors
- Not setting dp[0] = 0 explicitly
int coins[] = {1, 3, 4};
int amount = 6;
int dp[7];
for(int i=0; i<=amount; i++) dp[i] = amount+1;
dp[0] = 0;
for(int i=1; i<=amount; i++) {
for(int j=0; j<3; j++) {
if(coins[j] <= i) {
dp[i] = dp[i] < dp[i - coins[j]] + 1 ? dp[i] : dp[i - coins[j]] + 1;
}
}
}
printf("%d\n", dp[amount]);Solution
Step 1: Trace dp array updates for amount 6
Calculate minimum coins for each amount: - dp[0]=0 - dp[1]=1 (1 coin) - dp[2]=2 (1+1) - dp[3]=1 (3 coin) - dp[4]=1 (4 coin) - dp[5]=2 (4+1) - dp[6]=2 (3+3)Step 2: Final dp[6] value
dp[6] = 2, minimum coins needed to make 6.Final Answer:
2 -> Option CQuick Check:
Minimum coins for 6 = 2 [OK]
- Forgetting to update dp[i] with minimum value
- Incorrectly handling coin indices
- Assuming dp[i] equals i always
int coins[] = {1, 2, 5};
int amount = 11;
int dp[12];
for(int i=0; i<=amount; i++) dp[i] = 0;
dp[0] = 0;
for(int i=1; i<=amount; i++) {
for(int j=0; j<3; j++) {
if(coins[j] <= i) {
dp[i] = dp[i - coins[j]] + 1;
}
}
}
printf("%d\n", dp[amount]);Solution
Step 1: Check dp initialization
dp array is initialized with zeros, which incorrectly suggests zero coins needed for all amounts.Step 2: Correct initialization needed
dp should be initialized with a large number (like INT_MAX) to represent unreachable states, then updated with minimum coins.Final Answer:
dp array is not initialized with large values -> Option AQuick Check:
Initialize dp with max values to find minimum [OK]
- Setting dp to zero causes wrong minimum calculations
- Not updating dp[i] with minimum of previous values
- Incorrect loop ranges
Solution
Step 1: Check possible combinations for amount 7
Coins are 2, 5, 10. Possible sums: - 2+5=7 (2 coins) - 5+2=7 (2 coins) - 10 is too large So minimum coins = 2.Step 2: Confirm no smaller combination
Since 1-coin denominations are not available, 2 coins is minimum.Final Answer:
2 -> Option BQuick Check:
7 = 2 + 5, minimum coins = 2 [OK]
- Assuming 7 cannot be formed without 1-coin denomination
- Returning -1 incorrectly
- Ignoring order of coins
Ready to prove it?
Pass the test and this pattern moves unknown
