Proficient Level
Implement the method coinChangeMinCoins that returns the minimum number of coins needed for an amount.
You may use each coin value any number of times. Return the fewest coins needed to make the target amount exactly.
The task is designed to test careful handling of edge cases, not only the most common input.
- Use only the first
sizecoin values. - Return
0when the amount is0. - Return
-1when the amount cannot be formed.
Example 1
Input
coins (int[]) =
[1,2,5]size (int) =
3amount (int) =
11Returns (int)
3Example 2
Input
coins (int[]) =
[2]size (int) =
1amount (int) =
3Returns (int)
-1Example 3
Input
coins (int[]) =
[1,3,4]size (int) =
3amount (int) =
6Returns (int)
2Use dynamic programming where dp[x] stores the fewest coins needed for amount x.
Ready to test your solutionRun Code checks your solution against the test cases.Shift + Enter to run