Coin Change Problem

Coin Change Problem — ExecCode Medium DSA Practice

Solve the Coin Change problem on ExecCode. Free online medium DSA practice in DP. Write and run code in Java, C++, Python — no signup required to run.

Problem description

Given coins of different denominations and a total amount of money amount, return the fewest number of coins needed to make up that amount. You may use each denomination unlimited times. If it is impossible to make the amount with the given coins, return -1. The answer is guaranteed to fit in a 32-bit signed integer when it exists.

Examples

Input coins = [1, 2, 5], amount = 11; Output 3. Input coins = [2], amount = 3; Output -1. Input coins = [1], amount = 0; Output 0

Constraints

1 ≤ coins.length ≤ 12 1 ≤ coins[i] ≤ 2³¹-1 0 ≤ amount ≤ 10⁴

Practice Coin Change free on ExecCode. Browse DSA problems, topic map, and placement guides.