Skip to content
BytePatterns

Ways to Pay an Amount

MediumDynamic Programming#unbounded-knapsack#bottom-up-dp~25m

Problem

A vending machine accepts coins of a few distinct positive values, and it has an unlimited supply of each. Given the coin values and an amount, return how many different collections of coins add up to exactly that amount. The order in which coins are inserted does not matter, so 2 + 3 and 3 + 2 are the same collection, and an amount of 0 has exactly one collection: no coins.

Examples

Input:  coins = [2, 3, 5], amount = 10
Output: 4
Why:    2+2+2+2+2, 2+2+3+3, 5+5 and 2+3+5
Input:  coins = [4], amount = 6
Output: 0
Why:    no number of fours makes six
Input:  coins = [3, 7], amount = 0
Output: 1
Why:    edge case, the empty collection pays nothing

Hints

0 / 3

Stuck on the idea rather than the code? Counting Ways, Not Coins covers it.