In the greedy algorithms lesson you saw that “always take the biggest coin” can give the wrong answer for some coin systems. Dynamic programming always gets it right. This lesson solves the coin change problem two ways: the fewest coins to make an amount, and the number of different ways to make it. It then shows that the same idea solves the unbounded knapsack, where each item can be used as many times as you like.
In this lesson you will learn
- A one-dimensional DP for the fewest coins
- How to rebuild which coins were used
- How to count combinations and sequences
- Why the order of the two loops changes the answer
- The unbounded knapsack and how it differs from 0/1 knapsack
You should know dynamic programming and the 0/1 knapsack.
Fewest Coins
Let best[a] be the fewest coins that add up to amount a. To make a, the last coin used is some coin c, and the rest is a − c. So:
best[0] = 0
best[a] = 1 + min over coins c ≤ a of best[a − c]
#include <stdio.h>
#define INF 1000000
/* Fewest coins to make each amount (unlimited coins of each type) */
int main(void) {
int coins[] = {1, 3, 4};
int nc = 3, target = 6;
int best[16], used[16];
best[0] = 0;
for (int a = 1; a <= target; a++) {
best[a] = INF; used[a] = -1;
for (int c = 0; c < nc; c++)
if (coins[c] <= a && best[a - coins[c]] + 1 < best[a]) {
best[a] = best[a - coins[c]] + 1;
used[a] = coins[c];
}
}
printf("amount: ");
for (int a = 0; a <= target; a++) printf("%3d", a);
printf("\ncoins: ");
for (int a = 0; a <= target; a++) printf("%3d", best[a]);
printf("\n\nFewest coins for %d: %d (", target, best[target]);
for (int a = target; a > 0; a -= used[a]) printf("%d%s", used[a], a - used[a] > 0 ? " + " : "");
printf(")\n");
printf("Greedy (largest first) would use 4 + 1 + 1 = 3 coins.\n");
return 0;
}
Output
amount: 0 1 2 3 4 5 6
coins: 0 1 2 1 1 2 2
Fewest coins for 6: 2 (3 + 3)
Greedy (largest first) would use 4 + 1 + 1 = 3 coins.
- The table is filled from 0 upwards, so
best[a − c]is always ready when we need it. used[a]records the last coin chosen for each amount. Following it backwards from 6 gives 3, then 3, then 0.- With coins {1, 3, 4}, greedy picks 4 first and then needs two 1s. DP finds 3 + 3, which uses one coin fewer.
- If no combination works, best[a] stays at the large INF value, which you can report as “impossible”.
- The time is O(amount × number of coins).
Counting the Ways
To count ways, add instead of taking the minimum: ways[a] += ways[a − c]. But there is a subtle choice about what counts as “different”.
#include <stdio.h>
/* Count the number of ways to make an amount.
Coins in the OUTER loop counts combinations (order does not matter). */
static long combinations(const int *coins, int nc, int target) {
long ways[64] = {0};
ways[0] = 1;
for (int c = 0; c < nc; c++)
for (int a = coins[c]; a <= target; a++)
ways[a] += ways[a - coins[c]];
return ways[target];
}
/* Amount in the OUTER loop counts sequences (order matters). */
static long sequences(const int *coins, int nc, int target) {
long ways[64] = {0};
ways[0] = 1;
for (int a = 1; a <= target; a++)
for (int c = 0; c < nc; c++)
if (coins[c] <= a) ways[a] += ways[a - coins[c]];
return ways[target];
}
/* Unbounded knapsack: unlimited copies of each item */
static int unbounded(const int *w, const int *v, int n, int cap) {
int best[64] = {0};
for (int c = 1; c <= cap; c++)
for (int i = 0; i < n; i++)
if (w[i] <= c && best[c - w[i]] + v[i] > best[c])
best[c] = best[c - w[i]] + v[i];
return best[cap];
}
int main(void) {
int coins[] = {1, 2, 5};
printf("Ways to make 5 with {1, 2, 5}:\n");
printf(" combinations (order ignored): %ld\n", combinations(coins, 3, 5));
printf(" sequences (order matters): %ld\n", sequences(coins, 3, 5));
int w[] = {2, 3, 4}, v[] = {3, 5, 7};
printf("\nUnbounded knapsack, capacity 10: best value %d\n", unbounded(w, v, 3, 10));
return 0;
}
Output
Ways to make 5 with {1, 2, 5}:
combinations (order ignored): 4
sequences (order matters): 9
Unbounded knapsack, capacity 10: best value 17
- Coins in the outer loop: every coin type is fully processed before the next. A way is built in coin order, so 1 + 2 + 2 and 2 + 1 + 2 count as one. The four combinations for 5 are 5, 2+2+1, 2+1+1+1 and 1+1+1+1+1.
- Amount in the outer loop: for each amount, any coin can be the last one. Now 1 + 2 + 2, 2 + 1 + 2 and 2 + 2 + 1 are all different, giving 9 sequences. This is like counting ways to climb stairs with steps of 1, 2 or 5.
- Swapping two loops changes the meaning completely. Always ask whether order matters.
Unbounded Knapsack
The 0/1 knapsack lets you take each item at most once. In the unbounded version, each item can be taken any number of times, just like coins.
best[c]is the highest value that fits in capacity c.- For each item that fits, try adding it to the best answer for c − weight.
- With items (weight 2, value 3), (3, 5) and (4, 7), the best for capacity 10 is 17, for example 4 + 4 + 2 (7 + 7 + 3).
| 0/1 knapsack | Unbounded knapsack | |
|---|---|---|
| Copies of each item | At most one | Any number |
| 1D loop over capacity | Goes downwards, so an item is not reused | Goes upwards, so an item can be reused |
| Example | Packing a bag of different objects | Cutting a rod, making change |
Common Mistakes
- Using greedy for coin systems where it fails.
- Forgetting
ways[0] = 1: there is exactly one way to make 0, by taking no coins. - Putting the loops in the wrong order, counting sequences when combinations were wanted, or the reverse.
- Adding 1 to INF, which can overflow; test for INF first or use a safe large value.
Practice Exercises
Exercise 1. With coins {2, 5}, what is the fewest number of coins for 3? For 9?
Show answer
3 is impossible (best stays INF). 9 = 5 + 2 + 2, so 3 coins.
Exercise 2. How many combinations make 4 with coins {1, 2}?
Show answer
Three: 2+2, 2+1+1 and 1+1+1+1.
Frequently Asked Questions
When does greedy work for coins?
For “canonical” systems such as Bangladeshi taka notes (1, 2, 5, 10, 20, 50, 100, …), greedy always gives the fewest notes. For arbitrary coin values it can fail, so DP is the safe choice.
Can the counts get very large?
Yes. The number of ways grows quickly, so problems often ask for the answer modulo 1 000 000 007. Take the remainder after each addition.
Summary
- Fewest coins: best[a] = 1 + min of best[a − c].
- Counting ways: add ways[a − c] instead of taking a minimum.
- Coins outside the loop count combinations; amounts outside count sequences.
- Unbounded knapsack is the same idea with values; loop capacity upwards.
Next: Sparse Table in C.
