Minimum Number of Coins
Mediumjavapythonccppjavascript
Given coin denominations and a target amount, find the minimum number of coins needed to make exactly that amount. Each denomination may be used any number of times.
Input Format
The first line contains an integer N, the number of denominations. The second line contains N space-separated positive integers — the coin values. The third line contains an integer amount.
Output Format
Print the minimum number of coins, or -1 if the amount cannot be formed.
Example 1
Input
4 1 5 10 25 30
Output
2
Explanation: 25 + 5 = 30, so two coins suffice.
- 1 <= N <= 100
- 1 <= coin value <= 10000
- 0 <= amount <= 10000