#322

Coin Change

specialist · 895 · lc medium +31 · verified · 48% accepted · 20,852 likes · top 33%

play →

Description

An array coins holds denominations of available currency, with unlimited coins of each type. Given a target integer amount, find the smallest number of coins whose values sum to exactly amount. If no valid combination can reach amount, return -1.

Example 1:

Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1

Example 2:

Input: coins = [2], amount = 3
Output: -1

Example 3:

Input: coins = [1], amount = 0
Output: 0

Code

1
2
3