InferenceAI

Enter at least two characters to search.

Start free

Coin Change Ways

mediumDynamic Programming25 min25 XP

Return the number of combinations (order doesn't matter) of coins from coins (unlimited supply) that sum to amount.

Requirements

  • Loop over coins in the outer loop so each combination is counted once.

Examples

EXAMPLE WALKTHROUGHfour ways

A guided view of the published example, not a trace of your code.

INPUT[5,[1,2,5]]
TASKcoin_change_ways()
EXPECTED OUTPUT4

Start with the input values shown in this example.

Example 1 of 3

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

Output: 4

Explanation: four ways

Input: amount = 3, coins = [2]

Output: 0

Explanation: impossible

Input: amount = 10, coins = [10]

Output: 1

Explanation: one coin

Hint 1

ways[a] += ways[a - coin] for every amount a >= coin, processing one coin at a time.

Topics

DSADynamic ProgrammingPython