-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsolution.cpp
46 lines (44 loc) · 1.07 KB
/
solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* 15 / 15 test cases passed.
* Runtime: 0 ms
* Memory Usage: 6.8 MB
*/
class Solution {
public:
unordered_map<int, int> memo;
int dfs(vector<int>& nums, int target) {
if (target == 0) return 1;
if (memo.count(target)) return memo[target];
int res = 0;
for (int i = 0; i < nums.size(); ++ i) {
if (nums[i] <= target) {
res += dfs(nums, target - nums[i]);
}
}
memo[target] = res;
return res;
}
int combinationSum4(vector<int>& nums, int target) {
return dfs(nums, target);
}
};
/**
* 15 / 15 test cases passed.
* Runtime: 0 ms
* Memory Usage: 6.4 MB
*/
class Solution2 {
public:
int combinationSum4(vector<int>& nums, int target) {
vector<int> dp(target + 1);
dp[0] = 1;
for (int i = 1; i <= target; ++ i) {
for (auto& num: nums) {
if (i >= num) {
dp[i] = (0LL + dp[i] + dp[i - num]) % INT_MAX;
}
}
}
return dp[target];
}
};