-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
200 additions
and
0 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
...n/java/problemsolving/leetcode/algorithm/dynamicProgramming/targetSum/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
24 changes: 24 additions & 0 deletions
24
...a/problemsolving/leetcode/algorithm/dynamicProgramming/targetSum/TargetSumBruteForce.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...msolving/leetcode/algorithm/dynamicProgramming/targetSum/TargetSumDynamicProgramming.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...oblemsolving/leetcode/algorithm/dynamicProgramming/targetSum/TargetSumBruteForceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...ving/leetcode/algorithm/dynamicProgramming/targetSum/TargetSumDynamicProgrammingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |