Skip to content
BytePatterns

Exact Change With Unlimited Coins

EasyDynamic Programming#unbounded-knapsack#bottom-up-dp~15m

Problem

A vending machine holds an unlimited supply of coins in each denomination listed in coins. Return True if it can pay back exactly amount, otherwise False.

Examples

Input:  coins = [4, 7], amount = 15
Output: True
Why:    4 + 4 + 7 = 15
Input:  coins = [4, 6], amount = 9
Output: False
Why:    every mix of 4s and 6s is even
Input:  coins = [5], amount = 0
Output: True
Why:    edge case, paying zero needs no coins

Hints

0 / 3

Stuck on the idea rather than the code? Coin Change covers it.