Algorithms Playbook
← All problems
Medium1D dynamic programming →Similar to: Coin Change

Fewest coins

Problem

Given distinct coin values coins (each usable any number of times) and an integer amount, return the fewest coins needed to make exactly amount. If it cannot be made, return -1.

Examples

Input: coins = [1, 3, 4], amount = 6

Output: 2

3 + 3. Greedy (4+1+1) would use 3.

Input: coins = [2], amount = 3

Output: -1

Input: coins = [1], amount = 0

Output: 0

Hints

    Try it first. Every hint reveals a bit more.

    Solution