Skip to content

Commit 04a8587

Browse files
Target Sum: Accepted
1 parent 62e873a commit 04a8587

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ My accepted leetcode solutions to some of the common interview problems.
4343
- [Remove Invalid Parentheses](problems/src/backtracking/RemoveInvalidParentheses.java) (Medium)
4444
- [Regular Expression Matching](problems/src/backtracking/RegularExpressionMatching.java) (Hard)
4545
- [Expression Add Operators](problems/src/backtracking/ExpressionAddOperators.java) (Hard)
46+
- [Expression Add Operators](problems/src/backtracking/ExpressionAddOperators.java) (Hard)
4647

4748
#### [Binary Search](problems/src/binary_search)
4849

@@ -54,7 +55,7 @@ My accepted leetcode solutions to some of the common interview problems.
5455
- [Median of Two Sorted Arrays](problems/src/binary_search/MedianOfTwoSortedArrays.java) (Hard)
5556
- [Pow(x, n)](problems/src/binary_search/PowXN.java) (Medium)
5657
- [Find Peak Element](problems/src/binary_search/FindPeakElement.java) (Medium)
57-
- [First Bad Version](problems/src/binary_search/FirstBadVersion.java) (Easy)
58+
- [Target Sum](problems/src/binary_search/TargetSum.java) (Medium)
5859

5960
#### [Bit Manipulation](problems/src/bit_manipulation)
6061

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package backtracking;
2+
3+
/**
4+
* Created by gouthamvidyapradhan on 09/12/2017.
5+
* You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symbols + and -.
6+
* For each integer, you should choose one from + and - as its new symbol.
7+
8+
Find out how many ways to assign symbols to make sum of integers equal to target S.
9+
10+
Example 1:
11+
Input: nums is [1, 1, 1, 1, 1], S is 3.
12+
Output: 5
13+
Explanation:
14+
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+
+1+1+1+1-1 = 3
20+
21+
There are 5 ways to assign symbols to make the sum of nums be target 3.
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 TargetSum {
29+
30+
private static int n;
31+
/**
32+
* Main method
33+
* @param args
34+
* @throws Exception
35+
*/
36+
public static void main(String[] args) throws Exception{
37+
int[] A = {1, 1, 1, 1, 1};
38+
n = 0;
39+
new TargetSum().findTargetSumWays(A, 3);
40+
System.out.println(n);
41+
}
42+
43+
public int findTargetSumWays(int[] nums, int S) {
44+
backtrack(nums, S, 0, 0);
45+
return n;
46+
}
47+
48+
private void backtrack(int[] nums, int target, int sum, int i){
49+
if(i == nums.length){
50+
if(sum == target){
51+
n++;
52+
}
53+
} else{
54+
backtrack(nums, target, sum + nums[i], i + 1);
55+
backtrack(nums, target, sum - nums[i], i + 1);
56+
}
57+
}
58+
59+
}
60+

0 commit comments

Comments
 (0)