Coin Change II - Complete Solution Guide
Coin Change II is LeetCode problem 518, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money. Return the number of combinations that make up that amount . If that amount of money cannot be made up by any combination of the coins, return 0 . You may assume that you have an infinite number of each kind of coin. The answer is guaranteed to fit into a signed 32-bit integer. Example 1: Input: amount = 5, coins = [1,2,5] Output: 4 Explanation: there are
Detailed Explanation
The problem asks us to find the number of distinct combinations of coins that add up to a given target amount. We are given an array of coin denominations and a target amount. We can assume an unlimited supply of each coin denomination. The key is to count the number of combinations, not permutations, meaning the order of the coins doesn't matter (e.g., 1+2 and 2+1 are considered the same combination). If no combination sums to the target amount, we should return 0.
Solution Approach
The solution uses dynamic programming to build a table `dp` where `dp[i]` stores the number of ways to make amount `i` using the given coins. We iterate through each coin and for each coin, we iterate through the amounts from the coin's value up to the target amount. For each amount `i`, we add the number of ways to make `i - coin` to `dp[i]`. This is because if we use the current coin, the number of combinations to reach amount 'i' are increased by the number of combinations to reach amount `i-coin`.
Step-by-Step Algorithm
- Step 1: Initialize a `dp` array of size `amount + 1` with all elements set to 0. This array will store the number of combinations for each amount from 0 to `amount`.
- Step 2: Set `dp[0] = 1`. This is the base case because there is one way to make an amount of 0 (using no coins).
- Step 3: Iterate through each coin in the `coins` array.
- Step 4: For each coin, iterate through the amounts from `coin` to `amount` (inclusive).
- Step 5: For each amount `i`, update `dp[i]` by adding `dp[i - coin]` to it. This represents adding the current coin to existing combinations that sum to `i - coin`.
- Step 6: After iterating through all coins and amounts, `dp[amount]` will contain the total number of combinations to make up the target amount. Return `dp[amount]`.
Key Insights
- Insight 1: This is a classic dynamic programming problem. We need to store the number of combinations for each amount from 0 up to the target amount.
- Insight 2: We can use a 1D array `dp` where `dp[i]` represents the number of combinations to make up amount `i`. The base case is `dp[0] = 1` since there's one way to make an amount of 0 (using no coins).
- Insight 3: The order in which we iterate through the coins is crucial. We iterate through the coins first (outer loop) and then the amounts (inner loop) to ensure that we are counting combinations (order doesn't matter). If we iterate through the amounts first, we would be counting permutations.
Complexity Analysis
Time Complexity: O(n*m)
Space Complexity: O(m)
Topics
This problem involves: Array, Dynamic Programming.
Companies
Asked at: Morgan Stanley.