FastPrepCoin Change

Coin Change

Apple logoApple● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

You have an unlimited supply of each denomination in coins. Return the smallest number of coins whose total is amount.

Return -1 if no combination reaches the amount. Making amount 0 requires no coins.

Function

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

Examples

Example 1

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

Use 5 + 5 + 1.

Example 2

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

A sum of coins valued 2 cannot be odd.

Example 3

coins = [1]amount = 0return = 0

The empty selection has total zero.

Constraints

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

More Apple problems

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