322. Coin Change

Practice Coin Change for free on NudgeCode: Medium, Dynamic Programming pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.

You have unlimited coins of each value in coins. Return the fewest coins that add up to exactly amount, or -1 if it cannot be done.

Example

coins = [1,2,5], amount = 11  ->  3    (5 + 5 + 1)
coins = [2],     amount = 3   ->  -1

Constraints