FastPrepCoin Change II

Coin Change II

Apple logoApple● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

You have an unlimited supply of each distinct denomination in coins. Count the combinations that sum to amount.

Order does not distinguish combinations: using coins 1,2,2 is the same combination as using 2,1,2. Return 0 when no combination exists. Amount 0 has one combination, the empty selection.

The requested count fits in a signed 32-bit integer.

Function

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

Examples

Example 1

amount = 5coins = [1,2,5]return = 4

The combinations are [5], [2,2,1], [2,1,1,1], and [1,1,1,1,1].

Example 2

amount = 3coins = [2]return = 0

No combination of denomination 2 reaches an odd amount.

Example 3

amount = 10coins = [10]return = 1

Only one coin of denomination 10 is needed.

Constraints

  • 1 <= coins.length <= 300.
  • All denominations are distinct, with 1 <= coins[i] <= 5000.
  • 0 <= amount <= 5000.
  • The answer is at most 2^31 - 1.

More Apple problems

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