-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0494-target-sum.java
81 lines (69 loc) · 2.5 KB
/
0494-target-sum.java
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Solution Shown In Video
public class Solution {
HashMap<String, Integer> dp;
public int findTargetSumWays(int[] nums, int S) {
dp = new HashMap<>();
return calculate(nums, 0, 0, S);
}
public int calculate(int[] nums, int i, int sum, int S) {
String s = i + "," + sum;
if (i == nums.length) {
return (sum == S)? 1 : 0;
}
if(dp.containsKey(s)){
return dp.get(s);
}
int res = calculate(nums, i + 1, sum + nums[i], S) + calculate(nums, i + 1, sum - nums[i], S);
dp.put(s, res);
return res;
}
}
/* Alternative Better Complexity Solution
-------------------------------------------------------------------------------------------------------
//Brute-force solution (accepted)
// Subset Sum DP solution (Recursive DP solution for java exceeds time limit)
* Calculate for the sum of all the potential positive numbers (targetSum)
*
* Formula: targetSum = (∑nums + target) / 2
* (must be even otherwise there's no valid answer so return 0)
*
* Return all the possible ways to get the targetSum
* since the remaining numbers would be negative we just need to account
* for the sum of the positive numbers (targetSum)
*
* The formula for the targetSum was derived as follows:
* P = potential positive numbers
* N = potential negative numbers
* ∑P - ∑N = target
* ∑P - ∑N + ∑nums = target + ∑nums
* ∑P - ∑N + (∑P + ∑N) = target + ∑nums
* ∑P + ∑P = target + ∑nums
* 2 * ∑P = target + ∑nums
* (2 * ∑P) / 2 = (target + ∑nums) / 2
* ∑P = (target + ∑nums) / 2
-------------------------------------------------------------------------------
class Solution {
public int subsetSum(int[] nums, int targetSum) {
int[] dp = new int[targetSum + 1];
dp[0] = 1;
for (int n : nums) {
for (int i = targetSum; i >= n; i--) {
dp[i] += dp[i - n];
}
}
return dp[targetSum];
}
public int findTargetSumWays(int[] nums, int target) {
int targetSum = 0;
for (int n : nums) {
targetSum += n;
}
return (targetSum < target
|| targetSum + target < 0
|| (targetSum + target) % 2 != 0)
? 0
: subsetSum(nums, (targetSum + target) / 2);
}
}
----------------------------------------------------------------------------------
*/