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:
- The length of the given array is positive and will not exceed 20.
- The sum of elements in the given array will not exceed 1000.
- Your output answer is guaranteed to be fitted in a 32-bit integer.
题解:
List some examples. e.g. 1,1,1,1,1.
If all of them are positive, it is 5, all negitative, it is -5. Beyond [-5,5], it can't happen, thus, the ways count is 0.
For this sum question, let dp[i] denotes the sum up to i, the count of ways.
Iterate each num in nums. For num, it could be + or -. say first 1. Then it could -1 or 1.
The ways to -1 is 1, ways to 1 is 1. It is because dp[0] accumlate to dp[1] and dp[-1].
递推时, 上个可能结果或加或减当前num得到新的结果, 不同ways的数目在新结果下累计. 只有对应count大于0时才可能是上个可能结果, because it would not be out of index.
起始值dp[0 + sum] = 1. 可能结果为0的不同ways数目是1. sum is offset. dp[0] means sum up to -sum.
Time Complexity: O(sum * n). sum是nums所有num的和. n = nums.length
Space: O(sum).
AC Java:
class Solution {
public int findTargetSumWays(int[] nums, int S) {
if(nums == null || nums.length == 0){
return 0;
}
int sum = 0;
for(int num : nums){
sum += num;
}
if(sum < S || -sum > S){
return 0;
}
int [] dp = new int[2*sum+1];
//sum相当于 offset
dp[0+sum] = 1;
for(int num : nums){
int [] next = new int[2*sum+1];
for(int k = 0; k<2*sum+1; k++){
if(dp[k] > 0){
next[k+num] += dp[k];
next[k-num] += dp[k];
}
}
dp = next;
}
return dp[sum + S];
}
}
Method 2:
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.
update新的dp时会用到上一轮前面的值,所以要从后往前更新. 这样更新时保证用到的都是上轮的值.
Time Complexity: O(sum*nums.length). sum是nums所有num的和.
Space: O(sum).
AC Java:
class Solution {
public int findTargetSumWays(int[] nums, int S) {
int sum = 0;
for(int num : nums){
sum += num;
}
if(S < -sum || S > sum || (S+sum)%2 != 0){
return 0;
}
return subSum(nums, (S+sum)/2);
}
private int subSum(int [] nums, int target){
int [] dp = new int[target+1];
dp[0] = 1;
for(int num : nums){
for(int i = target; i>=num; i--){
dp[i] += dp[i-num];
}
}
return dp[target];
}
}
LeetCode Target Sum的更多相关文章
- [LeetCode] Target Sum 目标和
You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symb ...
- Leetcode——Target Sum
Question You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you ha ...
- Leetcode之深度优先搜索(DFS)专题-494. 目标和(Target Sum)
Leetcode之深度优先搜索(DFS)专题-494. 目标和(Target Sum) 深度优先搜索的解题详细介绍,点击 给定一个非负整数数组,a1, a2, ..., an, 和一个目标数,S.现在 ...
- [Leetcode] DP -- Target Sum
You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symb ...
- LN : leetcode 494 Target Sum
lc 494 Target Sum 494 Target Sum You are given a list of non-negative integers, a1, a2, ..., an, and ...
- Longest subarray of target sum
2018-07-08 13:24:31 一.525. Contiguous Array 问题描述: 问题求解: 我们都知道对于subarray的问题,暴力求解的时间复杂度为O(n ^ 2),问题规模已 ...
- LeetCode:Path Sum I II
LeetCode:Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such ...
- [leetcode] Combination Sum and Combination SumII
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- 剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers)
剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers) https://leetcode.com/problems/sum-of-two-in ...
随机推荐
- Apache Kudu
Apache Kudu是由Cloudera开源的存储引擎,可以同时提供低延迟的随机读写和高效的数据分析能力.Kudu支持水平扩展,使用Raft协议进行一致性保证,并且与Cloudera Impala和 ...
- 计算机网络概述 传输层 TCP拥塞控制
TCP拥塞控制 计算机网络中的带宽.交换结点中的缓存和处理机等,都是网络的资源.在某段时间,若对网络中某一资源的需求超过了该资源所能提供的可用部分,网络的性能就会变坏.这种情况就叫做拥塞. 拥塞控制就 ...
- 【Head First Servlets and JSP】笔记11:cookie
容器如何知道客户是谁?(这并不是HTTP能实现的!IP地址不能唯一的标识用户,另外,非必要不采用HTTPS 继续mark孤傲苍狼的博客,百科全书 cookie——Header——字典——键值对—— 延 ...
- 九、搭建备份服务器 使用rsync服务
简介 Rsync是开源快速.多功能,可以实现全量和增量的本地或者远程数据同步备份的优秀工具.增量备份效率更高,可以同步内容也可以同步属性 [root@backup-41 ~]# rpm -qa rsy ...
- Django源码剖析
一.Django底层剖析之一次请求到响应的整个流程 As we all know,所有的Web应用,其本质上其实就是一个socket服务端,而用户的浏览器就是一个socket客户端 #!/usr/bi ...
- 20145210姚思羽《网络对抗》Web基础
20145210姚思羽<网络对抗>Web基础 实验后回答问题 (1)什么是表单 表但是与用户交互的窗口,负责采集网页中的数据,允许用户在表单中输入信息. (2)浏览器可以解析运行什么语言. ...
- UVA639 二叉树
题意:深度为n的二叉树每个节点上有个开关,初始为关闭,每当小球落在节点上都会改变开关的状态,问编号为m的小球最终会落在哪里. 思路:对于二叉树的节点k,左节点右节点的编号为2k,2k+1.只需由最后一 ...
- oracle修改密码和设置密码有效期
一.修改密码1)修改密码 sql>alter user user01 identified by password; 2)修改密码并unlock sql>alter user user01 ...
- SEM竞价数据基本分析方法
今天我们从账户数据表现来看一看怎样通过数据分析,判断账户出现的问题及解决思路.也欢迎大家提出意见,共同讨论进步. 首先我们从关键词报告来分析数据: 以上图数据为例.(设定该行业CPC均价为8) 先说下 ...
- Bluetooth Profile for iPhone from the functional perspectives
Classic Bluetooth Profile for iPhone from the functional perspectives Function Description BT Profil ...