Skip to content

Commit 4839599

Browse files
committed
TargetSum494
1 parent d563e6b commit 4839599

File tree

2 files changed

+106
-2
lines changed

2 files changed

+106
-2
lines changed

Diff for: README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@
4444
| [Graph](https://github.com/fluency03/leetcode-java/blob/master/src/graph) |
4545

4646

47-
# Total: 383
47+
# Total: 384
4848

4949
| Easy | Medium | Hard | - |
5050
|:-------:|:-------:|:----:|:-:|
51-
| 102 | 211 | 66 | 4 |
51+
| 102 | 212 | 66 | 4 |
5252

5353

5454
| Question | Solution | Difficulty |
@@ -342,6 +342,7 @@
342342
| [485. Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/MaxConsecutiveOnes485.java) | Easy |
343343
| [487. Max Consecutive Ones II](https://leetcode.com/problems/max-consecutive-ones-ii/description/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/MaxConsecutiveOnesII487.java) | Medium |
344344
| [490. The Maze](https://leetcode.com/problems/the-maze/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/TheMaze490.java) | Medium |
345+
| [494. Target Sum](https://leetcode.com/problems/target-sum/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/TargetSum494.java) | Medium |
345346
| [496. Next Greater Element I](https://leetcode.com/problems/next-greater-element-i/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/NextGreaterElementI496.java) | Easy |
346347
| [498. Diagonal Traverse](https://leetcode.com/problems/diagonal-traverse/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/DiagonalTraverse498.java) | Medium |
347348
| [503. Next Greater Element II](https://leetcode.com/problems/next-greater-element-ii/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/NextGreaterElementII503.java) | Medium |

Diff for: src/TargetSum494.java

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* You are given a list of non-negative integers, a1, a2, ..., an, and a
3+
* target, S. Now you have 2 symbols + and -. For each integer, you should
4+
* choose one from + and - as its new symbol.
5+
*
6+
* Find out how many ways to assign symbols to make sum of integers equal to
7+
* target S.
8+
*
9+
* Example 1:
10+
* Input: nums is [1, 1, 1, 1, 1], S is 3.
11+
* Output: 5
12+
*
13+
* Explanation:
14+
* -1+1+1+1+1 = 3
15+
* +1-1+1+1+1 = 3
16+
* +1+1-1+1+1 = 3
17+
* +1+1+1-1+1 = 3
18+
* +1+1+1+1-1 = 3
19+
*
20+
* There are 5 ways to assign symbols to make the sum of nums be target 3.
21+
*
22+
* Note:
23+
* The length of the given array is positive and will not exceed 20.
24+
* The sum of elements in the given array will not exceed 1000.
25+
* Your output answer is guaranteed to be fitted in a 32-bit integer.
26+
*/
27+
28+
public class TargetSum494 {
29+
public int findTargetSumWays(int[] nums, int S) {
30+
if (S > 1000 || S < -1000) return 0;
31+
int N = nums.length;
32+
int[][] dp = new int[N+1][2001];
33+
34+
dp[0][1000] = 1;
35+
for (int i=1; i<=N; i++) {
36+
int n = nums[i-1];
37+
for (int j=0; j<2001; j++) {
38+
dp[i][j] = (j+n < 2001 ? dp[i-1][j+n] : 0) + (j-n >=0 ? dp[i-1][j-n] : 0);
39+
}
40+
}
41+
42+
return dp[N][S + 1000];
43+
}
44+
45+
public int findTargetSumWays2(int[] nums, int S) {
46+
if (S > 1000 || S < -1000) return 0;
47+
int N = nums.length;
48+
int[] dp = new int[2001];
49+
50+
dp[1000] = 1;
51+
for (int i=1; i<=N; i++) {
52+
int n = nums[i-1];
53+
int[] tmp = new int[2001];
54+
for (int j=0; j<2001; j++) tmp[j] = dp[j];
55+
for (int j=0; j<2001; j++) {
56+
dp[j] = (j+n < 2001 ? tmp[j+n] : 0) + (j-n >=0 ? tmp[j-n] : 0);
57+
}
58+
}
59+
60+
return dp[S + 1000];
61+
}
62+
63+
64+
/**
65+
* https://leetcode.com/problems/target-sum/solution/
66+
*/
67+
public int findTargetSumWays3(int[] nums, int S) {
68+
int[][] dp = new int[nums.length][2001];
69+
dp[0][nums[0] + 1000] = 1;
70+
dp[0][-nums[0] + 1000] += 1;
71+
for (int i = 1; i < nums.length; i++) {
72+
for (int sum = -1000; sum <= 1000; sum++) {
73+
if (dp[i - 1][sum + 1000] > 0) {
74+
dp[i][sum + nums[i] + 1000] += dp[i - 1][sum + 1000];
75+
dp[i][sum - nums[i] + 1000] += dp[i - 1][sum + 1000];
76+
}
77+
}
78+
}
79+
return S > 1000 ? 0 : dp[nums.length - 1][S + 1000];
80+
}
81+
82+
83+
/**
84+
* https://leetcode.com/problems/target-sum/solution/
85+
*/
86+
public int findTargetSumWays4(int[] nums, int S) {
87+
int[] dp = new int[2001];
88+
dp[nums[0] + 1000] = 1;
89+
dp[-nums[0] + 1000] += 1;
90+
for (int i = 1; i < nums.length; i++) {
91+
int[] next = new int[2001];
92+
for (int sum = -1000; sum <= 1000; sum++) {
93+
if (dp[sum + 1000] > 0) {
94+
next[sum + nums[i] + 1000] += dp[sum + 1000];
95+
next[sum - nums[i] + 1000] += dp[sum + 1000];
96+
}
97+
}
98+
dp = next;
99+
}
100+
return S > 1000 ? 0 : dp[S + 1000];
101+
}
102+
103+
}

0 commit comments

Comments
 (0)