📘
Coin Change
MediumDynamic Programming
Given coin denominations and a target amount, return the fewest coins needed. Return -1 if not possible.
Example:
Input:
1,2,5 11
Output:
3
Reasoning:
- The target amount is 11 and the available coin denominations are 1, 2, and 5.
- To find the fewest coins needed, we can use a combination of the largest denomination (5) and smaller denominations to reach the target amount: 5+5+1=11.
- This combination requires 3 coins, which is the fewest number of coins needed to reach the target amount.
- Therefore, the output is 3, indicating that 11 can be made with 3 coins.
Constraints:
- 1 <= len(coins) <= 12
- 1 <= coins[i] <= 2^31 - 1
- 0 <= amount <= 10^4
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.