🪙Coin Change

Given a list of coin denominations and a desired amount, return the fewest number of coins that can make up the desired amount.

Examples

I: coins = [1, 2, 5], amount = 11
O: 3 (5 + 5 + 1) 

I: coins = [2], amount = 3
O: -1 (not possible) 

I: coins = [1], amount = 0 
O: 0 

Approach

The easiest way to come up with an approach is to see the search graph or the search tree.

Coin Change Search Tree

D&C

this algo is exponential and the exponent is the depth

if the recursion depth is too high, then this approach is not functional.

for example [2,3,6,7], 77 can cause a TLE on the OJ

time is bounded pseudo polynomial

Greedy Boost

significantly reduce the search depth

for example [1,2,4], 32000

DP

Last updated