You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.
Return the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11 Output: 3 Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3 Output: -1
Example 3:
Input: coins = [1], amount = 0 Output: 0
Constraints:
1 <= coins.length <= 121 <= coins[i] <= 231 - 10 <= amount <= 104This approach dynamically computes the fewest number of coins needed for each amount from 0 to the target amount. You initialize an array dp where dp[i] represents the fewest number of coins needed to form amount i. Start with dp[0] = 0 since zero coins are needed to make the amount zero. For other amounts, assume infinity until proven otherwise. Iterate through each amount up to amount and each coin, updating dp accordingly. The solution lies in dp[amount].
This C solution uses an array dp initialized to INT_MAX except for dp[0] which is 0, signifying no coins are needed to make zero amount. For each possible amount, we iterate through each coin, updating dp[i] to be the minimum number of coins found so far that can make up i. The answer is dp[amount] unless it remains as infinity (in which case it is impossible to form the amount).
C++
Java
Python
C#
JavaScript
Time Complexity: O(n * m), where n is the amount and m is the number of coins.
Space Complexity: O(n), for the array dp.
This BFS-based method considers each step as a level, ensuring that every level in the search tree corresponds to adding a different coin denomination yielding a new amount. It is implemented using a queue initialized with the target amount, repeatedly generating the next possible amounts by subtracting each coin in turn until the amount is zero or deemed unreachable.
This BFS solution uses a queue to attempt each feasible coin subtraction on a given amount. It initially pushes a pair into the queue: the current amount, and the number of steps (or coins) taken. The iteration through coins short-circuits when next_amount becomes zero, else it appends the resulting amounts — not yet checked — back into the queue.
Time Complexity: Approximately O(n * m), comparable to standard BFS, where n is the amount and m is the number of coins. Actual time can be higher based on processed nodes.
Space Complexity: O(n), limited by queue and visited set sizes, corresponding to different amounts explored.
| Approach | Complexity |
|---|---|
| Dynamic Programming Approach | Time Complexity: O(n * m), where |
| Breadth-First Search (BFS) Approach | Time Complexity: Approximately O(n * m), comparable to standard BFS, where |
Coin Change - Dynamic Programming Bottom Up - Leetcode 322 • NeetCode • 574,201 views views
Watch 9 more video solutions →Practice Coin Change with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor