Leetcode 494: Target Sum

You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and - as its new symbol.

Find out how many ways to assign symbols to make sum of integers equal to target S.

Example 1:

Input: nums is [1, 1, 1, 1, 1], S is 3. 
Output: 5
Explanation: 

-1+1+1+1+1 = 3
+1-1+1+1+1 = 3
+1+1-1+1+1 = 3
+1+1+1-1+1 = 3
+1+1+1+1-1 = 3

There are 5 ways to assign symbols to make the sum of nums be target 3.

 

Note:

  1. The length of the given array is positive and will not exceed 20.
  2. The sum of elements in the given array will not exceed 1000.
  3. Your output answer is guaranteed to be fitted in a 32-bit integer.
 1 public class Solution {
 2     // tricky dp: https://discuss.leetcode.com/topic/76264/short-java-dp-solution-with-explanation
 3     public int FindTargetSumWays(int[] nums, int S) {
 4         if (nums == null) { return 0; }
 5         int sum = 0;
 6         foreach (int num in nums) {
 7             sum += num;
 8         }
 9         
10         if (S < -sum || S > sum) { return 0; }
11         
12         int n = nums.Length;
13         int[,] f = new int[n + 1, 2 * sum + 1];
14         
15         f[0, 0 + sum] = 1;//It seems like make no sense, but it is the base value;
16        
17         for (int i = 1; i < n + 1; i++) {
18             //Option 1: easy to understand
19             for (int j = 0; j < 2 * sum + 1; j++) {
20                 //f[i][j] = f[i - 1][j - nums[i - 1]] + f[i - 1][j + nums[i - 1]]; 
21                 if (j - nums[i - 1] >= 0) {
22                     f[i, j] += f[i - 1, j - nums[i - 1]];
23                 }
24                 if (j + nums[i - 1] <= 2 * sum) {
25                     f[i, j] += f[i - 1, j + nums[i - 1]];
26                 }
27             }
28         }
29         
30         return f[n, sum + S];
31     }
32 }
33 
34 public class Solution1 {
35     public int FindTargetSumWays(int[] nums, int S) {
36         if (nums == null || nums.Length == 0) return 0;
37         
38         return DFS(nums, S, 0, 0, new Dictionary<string, int>());
39     }
40     
41     private int DFS(int[] nums, int S, int start, int cur, Dictionary<string, int> memo)
42     {
43         if (start >= nums.Length)
44         {
45             return cur == S ? 1 : 0;
46         }
47         
48         var key = start.ToString() + "-" + cur.ToString();
49         if (memo.ContainsKey(key)) return memo[key];
50         
51         int add = DFS(nums, S, start + 1, cur + nums[start], memo);
52         int minus = DFS(nums, S, start + 1, cur - nums[start], memo);
53         
54         memo[key] = add + minus;
55         
56         return memo[key];
57     }
58 }
59 
60 public class Solution2 {
61     public int FindTargetSumWays(int[] nums, int S) {
62         int result = 0;
63         
64         DFS(nums, S, 0, 0, ref result);
65         
66         return result;
67     }
68     
69     private void DFS(int[] nums, int S, int start, int cur, ref int ways)
70     {
71         if (start >= nums.Length)
72         {
73             if (cur == S) ways++;
74             return;
75         }
76         
77         DFS(nums, S, start + 1, cur + nums[start], ref ways);
78         DFS(nums, S, start + 1, cur - nums[start], ref ways);
79     }
80 }

 

posted @ 2018-01-29 06:10  逸朵  阅读(111)  评论(0)    收藏  举报