Facebook Pixel

Coin Change, Optimization

This is an unbounded knapsack problem: each coin denomination can be used unlimited times. We minimize coin count rather than count combinations.

You are given a list coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. Each coin can be used any number of times. If amount cannot be made up by any combination of the coins, return -1.

Example 1:

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

Output: 3

Explanation:

11 = 5 + 5 + 1, minimum of 3 total coins used

Example 2:

Input: coins = [3], amount = 1

Output: -1

Try it yourself

Invest in Yourself
Your new job is waiting. 83% of people that complete the program get a job offer. Unlock unlimited access to all content and features.
Go Pro