MediumArrayDynamic ProgrammingBreadth-First Search

Coin Change

LeetCode
1 approach, code in all languages

You are given a list of coin denominations and a target amount of money. You may use each denomination any number of times.

Return the fewest number of coins needed to make up the target amount exactly. If no combination of the given coins sums to the amount, return -1.

Example 1

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

Output: 3

The amount 11 can be formed as 5 + 5 + 1, which uses three coins and no smaller count is possible.

Example 2

Input: coins = [2], amount = 3

Output: -1

With only the denomination 2 available, the odd amount 3 can never be reached, so the answer is -1.

Constraints

  • 1 <= coins.length <= 12
  • 1 <= coins[i] <= 2^31 - 1
  • 0 <= amount <= 10^4
You've got the patterns

Patterns get you through the screen. Shipping gets you hired.

FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.

750+ engineers trained · frontend, backend, AWS & AI

August 15 · 0d left
Enroll Now