Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions cpp/0377-combination-sum-iv.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Given an array of distinct integers nums and a target integer 'target',
return the number of possible combinations that add up to target.

Ex. nums = [1,2,3] target = 4
Possible Combinations: (1,1,1,1) (1,1,2) (1,2,1) (1,3) (2,1,1,)
(2,2) (3,1).
So, total number of combinations possible is 7.

Time: O(n * m)
Space: O(m)
*/

class Solution {
public:
int combinationSum4(vector<int>& nums, int target) {
sort(nums.begin(), nums.end());
vector<unsigned int> dp(target+1, 0);
dp[0] = 1;
for(int total=1; total<=target; total++) {
for(int i=0; i<nums.size(); i++) {
if(nums[i] <= total) dp[total] += dp[total - nums[i]];
else break;
}
}
return dp[target];
}
};