![]() |
VOOZH | about |
Given an amount N and an infinite supply of coins of denominations stored in list S = {S1, S2, ..., Sm}, the task is to find how many different ways we can make change for the amount N. The order of coins doesn’t matter only combinations count.
For Example:
Input: N = 4, S = [1, 2, 3]
Output: 4
Possible combinations: {1,1,1,1}, {1,1,2}, {2,2}, {1,3}Input: N = 10, S = [2, 5, 3, 6]
Output: 5
Possible combinations: {2,2,2,2,2}, {2,2,3,3}, {2,2,6}, {2,3,5}, {5,5}
Let's explore different methods to solve this.
In this method, we use a 1D table to count combinations. It iteratively updates the number of ways to make each amount using available coins, building results from smaller subproblems.
4
Explanation:
In this method, the problem is solved recursively but results of subproblems are stored in a dictionary (memoization).
4
Explanation:
In this method, a 2D table is used where each cell stores the number of ways to make a certain amount using a specific number of coin types.
4
Explanation:
This method tries every possible combination of coins recursively without optimization. It’s easy to implement but inefficient for large inputs.
4
Explanation:
Please refer complete article on Dynamic Programming | Set 7 (Coin Change) for more details!