72. Coin Change

Given coin denominations coins (unlimited supply of each) and an amount, return the fewest coins needed to make that amount, or -1 if it cannot be made.

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
Explanation: Only even amounts can be made with 2s.
Hint 1

Greedy (largest coin first) fails for coins like [1, 3, 4] and amount 6.

Hint 2

dp[a] = 1 + min(dp[a − c]) over every coin c ≤ a.

Dynamic ProgrammingBFS