generated from eyamenko/dotnet-template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem48.cs
35 lines (31 loc) · 1.02 KB
/
Problem48.cs
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
namespace LeetCode;
/// <summary>
/// <see href="https://leetcode.com/problems/combination-sum-iv/">Combination Sum IV</see>.
/// </summary>
public static class Problem48
{
/// <summary>
/// Given an array of distinct integers `nums` and a target integer `target`, return the number of possible combinations that add up to `target`.
/// Time complexity: O(n * t).
/// Space complexity: O(t).
/// </summary>
/// <param name="nums">Array to traverse.</param>
/// <param name="target">Target integer.</param>
/// <returns>Number of possible combinations that add up to the target.</returns>
public static int CombinationSum4(int[] nums, int target)
{
var dp = new int[target + 1];
dp[0] = 1;
for (var i = 1; i <= target; i++)
{
for (var j = 0; j < nums.Length; j++)
{
if (nums[j] <= i)
{
dp[i] += dp[i - nums[j]];
}
}
}
return dp[target];
}
}