-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
54 lines (51 loc) · 1.53 KB
/
Program.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
using System.Linq;
namespace TargetSum
{
class Program
{
static void Main(string[] args)
{
int[] nums = {1, 1, 1, 1, 1};
int S = 3;
Console.WriteLine(FindTargetSumWays_DP(nums, S));
}
static int FindTargetSumWays_DFS(int[] nums, int S)
{
var res = 0;
DFS(nums, S, 0, ref res);
return res;
}
static void DFS(int[] nums, int S, int index, ref int res)
{
if (index == nums.Length)
{
if (S == 0)
res++;
return;
}
DFS(nums, S + nums[index], index + 1, ref res);
DFS(nums, S - nums[index], index + 1, ref res);
}
static int FindTargetSumWays_DP(int[] nums, int S)
{
var sum = nums.Sum();
if (S > sum || S < -sum) return 0;
var dp = new int[nums.Length + 1][];
for (int i = 0; i < dp.Length; i++)
dp[i] = new int[2 * sum + 1];
dp[0][sum] = 1;
for (int i = 1; i < dp.Length; i++)
{
for (int j = 0; j < dp[i].Length; j++)
{
if (j - nums[i - 1] >= 0)
dp[i][j] += dp[i - 1][j - nums[i - 1]];
if (j + nums[i - 1] < dp[i].Length)
dp[i][j] += dp[i - 1][j + nums[i - 1]];
}
}
return dp[^1][sum + S];
}
}
}