0% found this document useful (0 votes)
101 views1 page

Target Sum - LeetCode

The problem asks you to count the number of ways to assign plus and minus symbols to the integers in a given array nums such that the expression evaluates to a target sum, with the solution using dynamic programming to build up the count in a two dimensional array. An example is provided to illustrate finding the 5 ways to make the sum of [1,1,1,1,1] equal to the target of 3. Constraints on the input arrays and target are also provided.

Uploaded by

Austin Tirell
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views1 page

Target Sum - LeetCode

The problem asks you to count the number of ways to assign plus and minus symbols to the integers in a given array nums such that the expression evaluates to a target sum, with the solution using dynamic programming to build up the count in a two dimensional array. An example is provided to illustrate finding the 5 ways to make the sum of [1,1,1,1,1] equal to the target of 3. Constraints on the input arrays and target are also provided.

Uploaded by

Austin Tirell
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Problem List 0

Description Discussion (11) Solutions (2.1K) Submissions Java Auto

1 class Solution {
class Solution {
494. Target Sum 2     public int findTargetSumWays(int[] nums, int 
target) {
Medium 8.5K 302
3         
4         int sum = 0;
Amazon Uber Facebook
5         for(int n: nums) sum += n;
6         int[][] dp = new int[nums.length][2*sum+1];
You are given an integer array nums and an integer target .
7         dp[0][sum-nums[0]] = 1;
You want to build an expression out of nums by adding one of the 8         dp[0][sum+nums[0]] += 1;

symbols '+' and '-' before each integer in nums and then 9         
10         for(int i=1; i<nums.length; i++)
concatenate all the integers.
11         {
For example, if nums = [2, 1] , you can add a '+' before 2 and a 12             for(int j=-sum; j<=sum; j++)
'-' before 1 and concatenate them to build the expression "+2- 13             {

1" . 14                 if(dp[i-1][sum+j] > 0){
15                     dp[i][sum+j+nums[i]] +=  dp[i-1][sum
Return the number of different expressions that you can build, which +j];
evaluates to target . 16                     dp[i][sum+j-nums[i]] +=  dp[i-1][sum
+j];
  17                 }
18             }
Example 1: 19         }
20         return Math.abs(target) > sum ? 0 : dp[nums.
length-1][sum+target];
Input: nums = [1,1,1,1,1], target = 3
21     }
Output: 5
22 }
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
Testcase Result
+1 + 1 + 1 - 1 + 1 = 3
+1 + 1 + 1 + 1 - 1 = 3
Case 1 Case 2

Example 2: nums =

[1,1,1,1,1]
Input: nums = [1], target = 1
Output: 1
target =

  3

Constraints:

1 <= nums.length <= 20

0 <= nums[i] <= 1000

0 <= sum(nums[i]) <= 1000

-1000 <= target <= 1000

Accepted 424.6K Submissions 931K Acceptance Rate 45.6%

Seen this question in a real interview before? 1/4

Yes No

Similar Questions Console Run Submit

You might also like