Coin Change
Given an array coins of distinct coin denominations and an integer amount, return the fewest number of coins needed to make up that amount. You may use each denomination unlimited times. If the amount cannot be formed, return -1.
Open official problem prompt ↗Find the smallest possible number of coins whose values add up exactly to the target amount, or prove it is impossible.
Making change at a register: to hand over N cents in as few coins as possible you consider dropping the total by each coin you own and reuse the already-computed best for the remainder.
- Input
- coins = [1, 2, 5], amount = 11
- Output
- 3
- Why
- 11 = 5 + 5 + 1 uses three coins, and no combination uses fewer.
1 <= coins.length <= 121 <= coins[i] <= 2^31 - 10 <= amount <= 10^4