Coin Change

You are given an array coins of distinct coin values and a target amount. You have an unlimited number of coins of each value.

Return the fewest coins whose values add up to exactly amount. If no combination of coins makes the amount, return -1. An amount of 0 needs 0 coins.

Constraints

  • 1 <= coins.length <= 12
  • 1 <= coins[i] <= 10^4, all different
  • 0 <= amount <= 10^4

Examples

Example 1

Input: coins = [1,3,4], amount = 6
Output: 2

3 + 3. Taking the largest coin first gives 4 + 1 + 1, which needs three coins.

Example 2

Input: coins = [2], amount = 3
Output: -1

Coins of 2 only make even totals, so 3 can't be made.

Example 3

Input: coins = [1,2,5], amount = 0
Output: 0

An amount of 0 needs no coins at all.

JavaScript you'll use: Arrays: create, read, update Numbers, Math and integer tricks

/**
 * @param {number[]} coins
 * @param {number} amount
 * @return {number}
 */
function coinChange(coins, amount) {
  
}

Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.