Repository navigation
Expand file tree
/
Copy pathTarget Sum.java
More file actions
63 lines (54 loc) · 1.83 KB
/
Copy pathTarget Sum.java
File metadata and controls
63 lines (54 loc) · 1.83 KB
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
// Runtime: 5 ms (Top 89.09%) | Memory: 42.70 MB (Top 55.56%)
class Solution {
public int findTargetSumWays(int[] nums, int target) {
//Solution 1
int sum = 0;
for(int x : nums)
sum += x;
if(((sum - target) % 2 == 1) || (target > sum))
return 0;
int n = nums.length;
int s2 = (sum - target)/2;
int[][] t = new int[n + 1][s2 + 1];
t[0][0] = 1;
for(int i = 1; i < n + 1; i++) {
for(int j = 0; j < s2 + 1; j++) {
if(nums[i - 1] <= j)
t[i][j] = t[i-1][j] + t[i - 1][j - nums[i - 1]];
else
t[i][j] = t[i - 1][j];
}
}
return t[n][s2];
//Solution 2
// int sum = 0;
// for(int x : nums)
// sum += x;
// if(((sum - target) % 2 != 0) || (target > sum))
// return 0;
// int n = nums.length;
// int s2 = (sum - target)/2;
// int[][] t = new int[n + 1][s2 + 1];
// for(int i = 0; i < n + 1; i++) {
// for(int j = 0; j < s2 + 1; j++) {
// if(i == 0)
// t[i][j] = 0;
// if(j == 0)
// t[i][j] = 1;
// }
// }
// for(int i = 1; i < n + 1; i++) {
// for(int j = 1; j < s2 + 1; j++) {
// if((nums[i - 1] > j) || (nums[i - 1] == 0))
// t[i][j] = t[i - 1][j];
// else
// t[i][j] = t[i - 1][j] + t[i - 1][j - nums[i - 1]];
// }
// }
// int count = 0;
// for(int x : nums)
// if(x == 0)
// count++;
// return (int)(Math.pow(2,count)) * t[n][s2];
}
}