Skip to content

Commit

Permalink
Solved LeetCode Target Sum Problem
Browse files Browse the repository at this point in the history
  • Loading branch information
ghsatpute committed Apr 24, 2023
1 parent 33881a2 commit 8d88835
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# 494. Target Sum

You are given an integer array `nums` and an integer target.

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.

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".
Return the number of different expressions that you can build, which evaluates to target.

### Example 1
```
Input: nums = [1,1,1,1,1], target = 3
Output: 5
Explanation: There are 5 ways to assign symbols to make the sum of nums be target 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
+1 + 1 + 1 + 1 - 1 = 3
```

### Example 2
```
Input: nums = [1], target = 1
Output: 1
```

## Constraints
* `1 <= nums.length <= 20`
* `0 <= nums[i] <= 1000`
* `0 <= sum(nums[i]) <= 1000`
* `-1000 <= target <= 1000`

# Solution Reference
https://www.youtube.com/watch?v=g0npyaQtAQM&ab_channel=NeetCode
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package problemsolving.leetcode.algorithm.dynamicProgramming.targetSum;

public class TargetSumBruteForce {
private int[] nums;
private int target;
public int findTargetSumWays(int[] nums, int target) {
this.nums = nums;
this.target = target;

return findTargetSumWays(0, 0);
}

private int findTargetSumWays(int index, int total) {
if (index == nums.length) {
return total == target ? 1 : 0;
}
// We have two options, one to add current number and another to subtract it.
// Add current number and see how many ways we can achieve the sum
int sumWaysByAdding = findTargetSumWays(index + 1, total + nums[index]);
// Subtract current number and see how many ways we can achieve the sum
int sumWaysBySubtracting = findTargetSumWays(index + 1, total - nums[index]);
return sumWaysByAdding + sumWaysBySubtracting;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package problemsolving.leetcode.algorithm.dynamicProgramming.targetSum;

import java.util.HashMap;
import java.util.Map;

public class TargetSumDynamicProgramming {
private int[] nums;
private int target;
private Map<String, Integer> memo = new HashMap<>();
public int findTargetSumWays(int[] nums, int target) {
this.nums = nums;
this.target = target;

return findTargetSumWays(0, 0);
}

private int findTargetSumWays(int index, int total) {
if (index == nums.length) {
return total == target ? 1 : 0;
}

String key = index + "," + total;

if (memo.containsKey(key)) {
return memo.get(key);
}

// We have two options, one to add current number and another to subtract it.
// Add current number and see how many ways we can achieve the sum
int sumWaysByAdding = findTargetSumWays(index + 1, total + nums[index]);
// Subtract current number and see how many ways we can achieve the sum
int sumWaysBySubtracting = findTargetSumWays(index + 1, total - nums[index]);

memo.put(key, sumWaysByAdding + sumWaysBySubtracting);

return sumWaysByAdding + sumWaysBySubtracting;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package problemsolving.leetcode.algorithm.dynamicProgramming.targetSum;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

class TargetSumBruteForceTest {
@Test
public void testCase01() {
int[] nums = {1, 1, 1, 1, 1};
int target = 3;

int targetSumWays = new TargetSumBruteForce().findTargetSumWays(nums, target);

assertEquals(5, targetSumWays);
}

@Test
public void testCase02() {
int[] nums = {1};
int target = 1;

int targetSumWays = new TargetSumBruteForce().findTargetSumWays(nums, target);

assertEquals(1, targetSumWays);
}

@Test
public void testCase03() {
int[] nums = {1};
int target = 2;

int targetSumWays = new TargetSumBruteForce().findTargetSumWays(nums, target);

assertEquals(0, targetSumWays);
}

@Test
@Disabled("This takes 33 seconds. See DP solution for running this")
public void testCase04() {
int[] nums =
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1};
int target = 3;

int targetSumWays = new TargetSumBruteForce().findTargetSumWays(nums, target);

assertEquals(1037158320, targetSumWays);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package problemsolving.leetcode.algorithm.dynamicProgramming.targetSum;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

class TargetSumDynamicProgrammingTest {
@Test
public void testCase01() {
int[] nums = {1, 1, 1, 1, 1};
int target = 3;

int targetSumWays = new TargetSumDynamicProgramming().findTargetSumWays(nums, target);

assertEquals(5, targetSumWays);
}

@Test
public void testCase02() {
int[] nums = {1};
int target = 1;

int targetSumWays = new TargetSumDynamicProgramming().findTargetSumWays(nums, target);

assertEquals(1, targetSumWays);
}

@Test
public void testCase03() {
int[] nums = {1};
int target = 2;

int targetSumWays = new TargetSumDynamicProgramming().findTargetSumWays(nums, target);

assertEquals(0, targetSumWays);
}

@Test
public void testCase04() {
// This used to take ~33 seconds in brute force, not it takes around 30ms
int[] nums =
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1};
int target = 3;

int targetSumWays = new TargetSumDynamicProgramming().findTargetSumWays(nums, target);

assertEquals(1037158320, targetSumWays);
}
}

0 comments on commit 8d88835

Please sign in to comment.