Coin Change Problems

Pasted image 20250726171903.png

Problem

Key patterns

For min coins problems, A common, efficient solution to this problem is to use a greedy algorithm. In this situation, you repeatedly select the largest coin that isn’t larger than the remaining amount of change (more info here). However, this doesn’t work for arbitrary combinations of coins (consider 1¢, 6¢, and 10¢ coins and using a greedy algorithm to compute makeChange(12)). Therefore, we will instead focus on a generalized solution.

The user is discussing the "minimum coins" problem and highlighting a key limitation of the greedy algorithm approach. They correctly point out that while a greedy strategy works for standard coin denominations (like US or Euro currency), it fails for arbitrary coin sets. The example given (1¢, 6¢, and 10¢ coins to make 12¢) is a perfect illustration:

As the user states, the greedy algorithm is not a generalized solution for arbitrary coin denominations. They are therefore shifting the focus to a generalized solution, which typically implies a dynamic programming approach.

The user's statement sets the stage for a discussion or explanation of dynamic programming for the minimum coin change problem.

// Brute force solution. Go through every
// combination of coins that sum up to c to
// find the minimum number
private int[] coins = new int[]{10, 6, 1};
public int makeChange(int c) {
if (c == 0) return 0;
int minCoins = Integer.MAX_VALUE;
// Try removing each coin from the total and
// see how many more coins are required
for (int coin : coins) {
// Skip a coin if it’s value is greater
// than the amount remaining
if (c - coin >= 0) {
int currMinCoins = makeChange(c - coin);
if (currMinCoins < minCoins)
minCoins = currMinCoins;
}
}
// Add back the coin removed recursively
return minCoins + 1;
}

Things to observe

Pasted image 20250201183021.png

Pasted image 20250201183651.png

Solution

Variations

Coin Change 2

Pasted image 20250819123222.png