LeetCode Target Sum

原题链接在这里:https://leetcode.com/problems/target-sum/description/

题目:

You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and - as its new symbol.

Find out how many ways to assign symbols to make sum of integers equal to target S.

Example 1:

Input: nums is [1, 1, 1, 1, 1], S is 3.
Output: 5
Explanation: 

-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

There are 5 ways to assign symbols to make the sum of nums be target 3.

Note:

  1. The length of the given array is positive and will not exceed 20.
  2. The sum of elements in the given array will not exceed 1000.
  3. Your output answer is guaranteed to be fitted in a 32-bit integer.

题解:

DP问题. 储存到num时所有可能结果和对应的不同ways的数目. 用dp数组在储存, 最大是sum 最小是-sum. 所以开个2*sum+1的数组.

递推时, 上个可能结果或加或减当前num得到新的结果, 不同ways的数目在新结果下累计.  只有对应count大于0时才可能是上个可能结果.

起始值dp[0 + sum] = 1. 可能结果为0的不同ways数目是1. sum像个offset.

Time Complexity: O(sum*nums.length). sum是nums所有num的和.

Space: O(sum).

AC Java:

 1 class Solution {
 2     public int findTargetSumWays(int[] nums, int S) {
 3         if(nums == null || nums.length == 0){
 4             return 0;
 5         }
 6
 7         int sum = 0;
 8         for(int num : nums){
 9             sum += num;
10         }
11
12         if(sum < S || -sum > S){
13             return 0;
14         }
15
16         int [] dp = new int[2*sum+1];
17         //sum相当于 offset
18         dp[0+sum] = 1;
19         for(int num : nums){
20             int [] next = new int[2*sum+1];
21             for(int k = 0; k<2*sum+1; k++){
22                 if(dp[k] > 0){
23                     next[k+num] += dp[k];
24                     next[k-num] += dp[k];
25                 }
26             }
27             dp = next;
28         }
29         return dp[sum + S];
30     }
31 }

nums中一部分用的+号 相当于positive, 另一部分用的 - 号相当于negative. 分成两组.

sum(p) - sum(n) = target.

sum(p) + sum(n) + sum(p)-sum(n) = target + sum(p) + sum(n)

2*sum(p) = target + sum(nums)

相当于在nums中找有多少种subarray, subarray自身的和是(target + sum(nums))/2的问题.

subSum求解这个转化问题.

存储到当前数字得到所有结果ways数目. 为什么循环中i要从大到小呢. 这其实是dp的space compression. 跟新新的dp时会用到上一轮前面的值,所以要从后往前更新. 这样更新时保证用到的都是上轮的值.

Time Complexity: O(sum*nums.length). sum是nums所有num的和.

Space: O(sum).

AC Java:

 1 class Solution {
 2     public int findTargetSumWays(int[] nums, int S) {
 3         if(nums == null || nums.length == 0){
 4             return 0;
 5         }
 6
 7         int sum = 0;
 8         for(int num : nums){
 9             sum += num;
10         }
11
12         if(sum < S || -sum > S || (sum+S)%2==1){
13             return 0;
14         }
15         return subSum(nums, (sum+S)/2);
16     }
17
18     private int subSum(int [] nums, int sum){
19         int [] dp = new int[sum+1];
20         dp[0]=1;
21         for(int num : nums){
22             for(int i = sum; i>=num; i--){
23                 dp[i] += dp[i-num];
24             }
25         }
26         return dp[sum];
27     }
28 }
时间: 2024-10-08 14:18:07

LeetCode Target Sum的相关文章

[Leetcode] DP -- Target Sum

You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and - as its new symbol. Find out how many ways to assign symbols to make sum of integers

[leetcode]Combination Sum @ Python

原题地址:https://oj.leetcode.com/problems/combination-sum/ 题意: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited

LeetCode: Combination Sum [038]

[题目] Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (including target)

LeetCode: Combination Sum II [039]

[题目] Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will be

Leetcode:Combination Sum 子集和问题

Combination Sum: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (includ

LeetCode: Two Sum 题解

Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that

Leetcode | Combination Sum I &amp;&amp; II

Combination Sum I Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note:All numbers (includ

[leetcode]Two Sum @ Python

原题地址:http://oj.leetcode.com/problems/two-sum/ 题意:找出数组numbers中的两个数,它们的和为给定的一个数target,并返回这两个数的索引,注意这里的索引不是数组下标,而是数组下标加1.比如numbers={2,7,11,17}; target=9.那么返回一个元组(1,2).这道题不需要去重,对于每一个target输入,只有一组解,索引要按照大小顺序排列. 解题思路:1,由于要找到符合题意的数组元素的下标,所以先要将原来的数组深拷贝一份,然后排

[LeetCode] K sum(2Sum、3Sum、4Sum)

1 2Sum Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please not