Skip to content

Commit ec447be

Browse files
committed
Solution as on 06-08-2022 10:40 pm
1 parent 2f24e10 commit ec447be

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

0377. Combination Sum IV.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// 377.✅ Combination Sum IV
2+
3+
class Solution
4+
{
5+
public:
6+
vector<int> dp;
7+
8+
Solution()
9+
{
10+
dp.resize(1001);
11+
fill(dp.begin(), dp.end(), -1);
12+
}
13+
14+
int combinationSum4(vector<int> &nums, int target, int currSum = 0)
15+
{
16+
if (currSum > target)
17+
return 0;
18+
if (currSum == target)
19+
return 1;
20+
if (dp[currSum] != -1)
21+
return dp[currSum];
22+
int ways = 0;
23+
for (int i = 0; i < nums.size(); i++)
24+
{
25+
if (currSum + nums[i] <= target)
26+
ways += combinationSum4(nums, target, currSum + nums[i]);
27+
}
28+
return dp[currSum] = ways;
29+
}
30+
};

0 commit comments

Comments
 (0)