-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathCombinationSumIV.cpp
More file actions
67 lines (58 loc) · 1.78 KB
/
Copy pathCombinationSumIV.cpp
File metadata and controls
67 lines (58 loc) · 1.78 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// - Refer Solution here: https://leetcode.com/problems/combination-sum-iv/discuss/1166177/Short-and-Easy-w-Explanation-or-Optimization-from-Brute-Force-to-DP-Solution
// Recursive solution
class Solution {
public:
int combinationSum4(vector<int>& nums, int target) {
if(target == 0) {
return 1;
}
int ans = 0;
for(int i=0; i<nums.size(); i++) {
if(nums[i] <= target)
ans += combinationSum4(nums, target - nums[i]);
}
return ans;
}
};
// Time Compleiy - O(N * T)
// Space Complexity - O(T), T is the number of recursive calls
// Top-Down Approach - DP
class Solution {
public:
int combinationSum4(vector<int>& nums, int target) {
vector<int> dp(target + 1, -1);
return helper(nums, helper, dp);
}
int helper(vector<int>& nums, int target, vector<int>& dp) {
if(target == 0) return 1;
if(dp[target] != -1) return dp[target];
/// recursive code starts
dp[target] = 0;
for(int i=0; i<nums.size(); i++) {
if(nums[i] <= target) {
dp[target] += helper(nums, target - nums[i], dp);
}
}
return dp[target];
}
};
// Time Complexity - O(N * T)
// Space Complexity - O(T)
// Bottom-up solution - DP
class Solution {
public:
int combinationSum4(vector<int>& nums, int target) {
vector<uint> dp(target + 1);
dp[0] = 1;
for(int currTarget=1; currTarget <= target; currTarget++) {
for(auto &num: nums) {
if(num <= currTarget) {
dp[currTarget] += dp[currTarget - num];
}
}
}
return dp[target];
}
};
// Time Complexity - O(N * T)
// Space Complexity - O(T)