-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathCombination_Sum_IV.cpp
36 lines (35 loc) · 1.06 KB
/
Combination_Sum_IV.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
class Solution {
int combinationSum4Util(int indx, int sum, vector<int>& nums, vector<vector<int>>& dp) {
if(sum == 0) {
return 1;
}
if(dp[indx][sum] != -1) return dp[indx][sum];
int ret = 0;
for(int i = 0; i < (int)nums.size(); ++i) {
if(sum - nums[i] >= 0) {
ret += combinationSum4Util(i, sum - nums[i], nums, dp);
}
}
return ret;
}
public:
int combinationSum4(vector<int>& nums, int target) {
// Big TLE
/*
if(nums.empty()) return 0;
vector<vector<int>> dp((int)nums.size(), vector<int>(target + 1, -1));
return combinationSum4Util(0, target, nums, dp);
*/
vector<int> dp(target + 1, 0);
dp[0] = 1;
sort(nums.begin(), nums.end());
for (int i = 1; i <= target; ++i) {
for(int j = 0; j < (int)nums.size(); ++j) {
if(i - nums[j] >= 0) {
dp[i] += dp[i - nums[j]];
}
}
}
return dp[target];
}
};