This problem looks busy at first, but the accepted solution is built around one steady invariant. For 518. Coin Change 2, the solution in this repository is mainly a dynamic programming solution.
Guide
What?
Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: dynamic programming.
The notes already sitting in the source point us in the right direction:
- TLE
- 20 / 27 test cases passed.
Guide
When?
Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are change.
Guide
Why?
The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.
- The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(N^2), space: O(N)
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//TLE
02//20 / 27 test cases passed.
03class Solution {
04public:
05 vector<vector<int>> memo;
06
07 int change(int amount, vector<int>& coins, int start) {
08 //coin >= 1
09 if(start == coins.size()) return amount == 0;
10 //impossible
11 if(amount < 0) return 0;
12
13 if(memo[start][amount] != 0){
14 return memo[start][amount];
15 }
16
17 for(int times = 0; coins[start]*times <= amount; times++){
18 int tmp = change(amount-coins[start]*times, coins, start+1);
19 // cout << "coin: " << coins[start] << ", times: " << times << ", " << tmp << endl;
20 memo[start][amount] += tmp;
21 // memo[start][amount] += change(amount-coins[start]*times, coins, start+1);
22 }
23
24 // cout << "(" << start << ", " << amount << "): " << memo[start][amount] << endl;
25 return memo[start][amount];
26 }
27
28 int change(int amount, vector<int>& coins) {
29 int n = coins.size();
30 if(n == 0) return amount == 0;
31 //the range of amount is [0,5000]
32 memo = vector<vector<int>>(n, vector<int>(5001));
33 change(amount, coins, 0);
34
35 return memo[0][amount];
36 }
37};
38
39//O(N) space DP
40//Runtime: 4 ms, faster than 99.34% of C++ online submissions for Coin Change 2.
41//Memory Usage: 7.8 MB, less than 100.00% of C++ online submissions for Coin Change 2.
42//time: O(N^2), space: O(N)
43class Solution {
44public:
45 int change(int amount, vector<int>& coins) {
46 int n = coins.size();
47 //amount's range: [0,5000]
48 vector<int> dp(5001, 0);
49
50 dp[0] = 1;
51
52 for(int i = n-1; i >= 0; i--){
53 for(int sum = coins[i]; sum <= amount; sum++){
54 dp[sum] += dp[sum-coins[i]];
55 }
56 }
57
58 return dp[amount];
59 }
60};
61
62//unbounded knapsack problem
63//https://leetcode.com/problems/coin-change-2/discuss/99212/Knapsack-problem-Java-solution-with-thinking-process-O(nm)-Time-and-O(m)-Space
64//O(N^2) space DP
65//Runtime: 36 ms, faster than 35.46% of C++ online submissions for Coin Change 2.
66//Memory Usage: 26 MB, less than 14.29% of C++ online submissions for Coin Change 2.
67//time: O(N^2), space: O(N^2)
68class Solution {
69public:
70 int change(int amount, vector<int>& coins) {
71 int n = coins.size();
72 vector<vector<int>> dp(n+1, vector<int>(5001, 0));
73
74 //use 0 coin to make up amount = 0
75 dp[0][0] = 1;
76
77 //i: use coins[0...i-1]
78 for(int i = 1; i <= n; i++){
79 /*
80 dp[i][0] = 1;
81 for(int sum = 1; sum <= amount; sum++){
82 */
83 for(int sum = 0; sum <= amount; sum++){
84 /*
85 dp[i-1][sum]: not choose current coin
86 dp[i][sum-coins[i-1]]: choose current coin(again)
87 */
88 dp[i][sum] = dp[i-1][sum] + (sum >= coins[i-1] ? dp[i][sum-coins[i-1]] : 0);
89 }
90 }
91
92 //use coins[0...n-1] to make up amount
93 return dp[n][amount];
94 }
95};
Cost