Skip to content

Exact Change With Unlimited Coins

Easy#unbounded-knapsack#bottom-up-dp~15 min

The mission

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

Target time
O(len(coins) · amount)
Target space
O(amount)

Constraints

  • unlimited supply of every coin

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

Python 3Ctrl+Enter runs
Custom test case