Problem · Array
MediumGrubhub logoGrubhubFULLTIMEPHONE SCREEN

Problem statement

You are given an array coins of distinct positive coin denominations and a nonnegative integer amount.

You have an unlimited supply of every denomination. Return the minimum number of coins needed to make exactly amount. If no combination can make the amount, return -1.

An amount of 0 requires 0 coins.

Function

coinChange(coins: int[], amount: int) → int

Examples

Example 1

coins = [1,2,5]amount = 11return = 3

Two coins of denomination 5 and one coin of denomination 1 make 11 with three coins.

Example 2

coins = [2]amount = 3return = -1

No number of coins with denomination 2 can make the odd amount 3.

Example 3

coins = [1]amount = 0return = 0

The empty selection makes amount 0.

Constraints

  • 1 <= coins.length <= 12.
  • 1 <= coins[i] <= 2^31 - 1.
  • All denominations are distinct.
  • 0 <= amount <= 10000.

More Grubhub problems

drafts saved locally
public int coinChange(int[] coins, int amount) {
    // write your code here
}
coins[1,2,5]
amount11
expected3
checking account