coins lists the coin values you can use, and you have an endless supply of each. Pick coins that add up to exactly amount, using as few coins as possible, and return how many you used. If amount can't be reached at all, return -1.
amountis between 0 and 3,000. Anamountof0takes0coins.coinsholds between 1 and 12 different positive values, in no set order.- Grabbing the biggest coin first can give the wrong answer. Example 1 shows this.
Input: coins = [1, 3, 4], amount = 6
Output: 23 + 3 uses 2 coins. If you take 4 first, you get 4 + 1 + 1, which is 3 coins.
Input: coins = [2], amount = 3
Output: -1
Input: coins = [1], amount = 0
Output: 0