Skip to content

Commit 9bbcbce

Browse files
committed
Added task 494.
1 parent d225fc9 commit 9bbcbce

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package g0401_0500.s0494_target_sum;
2+
3+
// #Medium #Top_100_Liked_Questions #Array #Dynamic_Programming #Backtracking
4+
5+
public class Solution {
6+
public int findTargetSumWays(int[] nums, int s) {
7+
int sum = 0;
8+
s = Math.abs(s);
9+
for (int i = 0; i < nums.length; i++) {
10+
sum += nums[i];
11+
}
12+
// Invalid s, just return 0
13+
if (s > sum || (sum + s) % 2 != 0) {
14+
return 0;
15+
}
16+
17+
int dp[][] = new int[(sum + s) / 2 + 1][nums.length + 1];
18+
dp[0][0] = 1;
19+
// empty knapsack must be processed specially
20+
for (int i = 0; i < nums.length; i++) {
21+
if (nums[i] == 0) {
22+
dp[0][i + 1] = dp[0][i] * 2;
23+
} else {
24+
dp[0][i + 1] = dp[0][i];
25+
}
26+
}
27+
for (int i = 1; i < dp.length; i++) {
28+
for (int j = 0; j < nums.length; j++) {
29+
dp[i][j + 1] += dp[i][j];
30+
if (nums[j] <= i) {
31+
dp[i][j + 1] += dp[i - nums[j]][j];
32+
}
33+
}
34+
}
35+
return dp[(sum + s) / 2][nums.length];
36+
}
37+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
494\. Target Sum
2+
3+
Medium
4+
5+
You are given an integer array `nums` and an integer `target`.
6+
7+
You want to build an **expression** out of nums by adding one of the symbols `'+'` and `'-'` before each integer in nums and then concatenate all the integers.
8+
9+
* For example, if `nums = [2, 1]`, you can add a `'+'` before `2` and a `'-'` before `1` and concatenate them to build the expression `"+2-1"`.
10+
11+
Return the number of different **expressions** that you can build, which evaluates to `target`.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = \[1,1,1,1,1\], target = 3
16+
17+
**Output:** 5
18+
19+
**Explanation:**
20+
21+
There are 5 ways to assign symbols to make the sum of nums be target 3.
22+
-1 + 1 + 1 + 1 + 1 = 3
23+
+1 - 1 + 1 + 1 + 1 = 3
24+
+1 + 1 - 1 + 1 + 1 = 3
25+
+1 + 1 + 1 - 1 + 1 = 3
26+
+1 + 1 + 1 + 1 - 1 = 3
27+
28+
**Example 2:**
29+
30+
**Input:** nums = \[1\], target = 1
31+
32+
**Output:** 1
33+
34+
**Constraints:**
35+
36+
* `1 <= nums.length <= 20`
37+
* `0 <= nums[i] <= 1000`
38+
* `0 <= sum(nums[i]) <= 1000`
39+
* `-1000 <= target <= 1000`
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g0401_0500.s0494_target_sum;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void findTargetSumWays() {
11+
assertThat(new Solution().findTargetSumWays(new int[] {1, 1, 1, 1, 1}, 3), equalTo(5));
12+
}
13+
14+
@Test
15+
void findTargetSumWays2() {
16+
assertThat(new Solution().findTargetSumWays(new int[] {1}, 1), equalTo(1));
17+
}
18+
}

0 commit comments

Comments
 (0)