FastPrepCoin Change

Coin Change

Accenture logoAccenture● MediumINTERNOA
Learn

Problem statement

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

You may use each denomination any number of times. Return the minimum number of coins needed to make exactly amount. If no combination can make the amount, return -1.

Function

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

Examples

Example 1

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

The amount can be formed with 5 + 5 + 1.

Example 2

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

No number of 2-value coins can form 3.

Example 3

coins = [2,3,7]amount = 0return = 0

No coins are needed to form amount 0.

Constraints

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

More Accenture problems

See Accenture hiring insights
public int coinChange(int[] coins, int amount) {
    // Write your code here.
}
coins[1,2,5]
amount11
expected3
Checking account…