Skip to content

Latest commit

 

History

History

518

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

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 four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1

Example 2:

Input: amount = 3, coins = [2]
Output: 0
Explanation: the amount of 3 cannot be made up just with coins of 2.

Example 3:

Input: amount = 10, coins = [10]
Output: 1

 

Constraints:

  • 1 <= coins.length <= 300
  • 1 <= coins[i] <= 5000
  • All the values of coins are unique.
  • 0 <= amount <= 5000

Companies: Uber, Amazon, Facebook, Google, Bloomberg, Apple, Goldman Sachs, Cisco, Microsoft, eBay, Adobe, Citadel

Related Topics:
Array, Dynamic Programming

Similar Questions:

Solution 1. DFS + Memo

// OJ: https://leetcode.com/problems/coin-change-2/
// Author: github.com/lzl124631x
// Time: O(A * C^2)
// Space: O(AC)
class Solution {
private:
    unordered_map<int, int> memo;
    int change(int amount, vector<int>& coins, int start) {
        if (!amount) return 1;
        if (start >= coins.size()) return 0;
        int key = amount * 1000 + start;
        if (memo.find(key) != memo.end()) return memo[key];
        int ans = 0;
        for (int i = start; i < coins.size(); ++i) {
            if (amount >= coins[i]) ans += change(amount - coins[i], coins, i);
        }
        return memo[key] = ans;
    }
public:
    int change(int amount, vector<int>& coins) {
        return change(amount, coins, 0);
    }
};

Solution 2. DP (Unbounded knapsack problem), Naive version

This is a typical unbounded knapsack problem where you can pick item unlimited times (unbounded).

Let dp[i+1][T] be the ways to form T value using A[0] ... A[i].

dp[i+1][T]      = dp[i][T] + dp[i][T-A[i]] + dp[i][T-2*A[i]] ...

dp[i][0] = 1

We can directly apply this formula.

This is the naive solution, and in the next solution we find way to optimize it.

// OJ: https://leetcode.com/problems/coin-change-2/
// Author: github.com/lzl124631x
// Time: O(N^2 * T)
// Space: O(NT)
class Solution {
public:
    int change(int T, vector<int>& A) {
        int N = A.size();
        vector<vector<int>> dp(N + 1, vector<int>(T + 1));
        for (int i = 0; i <= N; ++i) dp[i][0] = 1;
        for (int i = 0; i < N; ++i) {
            for (int t = 1; t <= T; ++t) {
                for (int k = 0; t - k * A[i] >= 0; ++k) {
                    dp[i + 1][t] += dp[i][t - k * A[i]];
                }
            }
        }
        return dp[N][T];
    }
};

Solution 3. DP (Unbounded knapsack problem)

Let dp[i+1][T] be the ways to form T value using A[0] ... A[i].

dp[i+1][T]      = dp[i][T] + dp[i][T-A[i]] + dp[i][T-2*A[i]] ...

dp[i+1][T-A[i]] =            dp[i][T-A[i]] + dp[i][T-2*A[i]] ...

// so
dp[i+1][T] = dp[i+1][T-A[i]] + dp[i][T]

dp[i][0] = 1
// OJ: https://leetcode.com/problems/coin-change-2/
// Author: github.com/lzl124631x
// Time: O(NT)
// Space: O(NT)
class Solution {
public:
    int change(int T, vector<int>& A) {
        int N = A.size();
        vector<vector<int>> dp(N + 1, vector<int>(T + 1));
        for (int i = 0; i <= N; ++i) dp[i][0] = 1;
        for (int i = 0; i < N; ++i) {
            for (int t = 1; t <= T; ++t) {
                dp[i + 1][t] = (t - A[i] >= 0 ? dp[i + 1][t - A[i]] : 0) + dp[i][t];
            }
        }
        return dp[N][T];
    }
};

Solution 4. DP (Unbounded knapsack problem) with Space Optimization

Since dp[i+1][T] only depends on dp[i+1][T-A[i]] and dp[i][T], we can reduce the dp array from N * T to 1 * T.

// OJ: https://leetcode.com/problems/coin-change-2/
// Author: github.com/lzl124631x
// Time: O(NT)
// Space: O(T)
class Solution {
public:
    int change(int T, vector<int>& A) {
        int N = A.size();
        vector<int> dp(T + 1);
        dp[0] = 1;
        for (int i = 0; i < N; ++i) {
            for (int t = A[i]; t <= T; ++t) {
                dp[t] += dp[t - A[i]];
            }
        }
        return dp[T];
    }
};