lc 494 Target Sum


494 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 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 Accepted

数组的数字要么取正要么取负,sum(P)代表所有取正的数的和,sum(N)代表所有取负的数的和。

                sum(P) - sum(N) = target
sum(P) + sum(N) + sum(P) - sum(N) = target + sum(P) + sum(N)
2 * sum(P) = target + sum(nums)

可以看出:如果target + sum(nums)为奇数,那必定可行方法数为0。之后利用 416 Partition Equal Subset Sum中的方法,即可求得数组中求和得到(target + sum(nums))/2的方法数。

class Solution {
public:
int findTargetSumWays(vector<int>& nums, int S) {
int sum = accumulate(nums.begin(), nums.end(), 0);
if (sum < S) return 0;
else return (S+sum) & 1 ? 0 : subsum(nums, (S+sum)>>1);
} int subsum(vector<int>& nums, int S) {
vector<int> dp(S+1, 0);
dp[0] = 1;
for (int n : nums)
for (int i = S; i >= n; i--)
dp[i] += dp[i-n];
return dp[S];
}
};

LN : leetcode 494 Target Sum的更多相关文章

  1. [LeetCode] 494. Target Sum 目标和

    You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symb ...

  2. Leetcode 494 Target Sum 动态规划 背包+滚动数据

    这是一道水题,作为没有货的水货楼主如是说. 题意:已知一个数组nums {a1,a2,a3,.....,an}(其中0<ai <=1000(1<=k<=n, n<=20) ...

  3. [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 ...

  4. LC 494. Target Sum

    问题描述 You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 ...

  5. 【LeetCode】494. Target Sum 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  6. 【leetcode】494. Target Sum

    题目如下: 解题思路:这题可以用动态规划来做.记dp[i][j] = x,表示使用nums的第0个到第i个之间的所有元素得到数值j有x种方法,那么很容易得到递推关系式,dp[i][j] = dp[i- ...

  7. 494. Target Sum - Unsolved

    https://leetcode.com/problems/target-sum/#/description You are given a list of non-negative integers ...

  8. 494. Target Sum

    You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symb ...

  9. 494. Target Sum 添加标点符号求和

    [抄题]: You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have ...

随机推荐

  1. PM12条

    PM首先是用户 站在用户角度看待问题 用户体验是一个完整的过程 追求效果,不做没用的东西 发现需求,而不是创造需求 决定不做什么,往往比决定做什么更重要 用户是很难被教育的,要迎合用户,而不是改变用户 ...

  2. iOS 中代码获取当前版本号

    [1]概念 iOS的版本号,一个叫做Version,一个叫做Build,这两个值都可以在Xcode 中选中target,点击“Summary”后看到. Version在plist文件中的key是“CF ...

  3. Tomcat启动报:invalid LOC header (bad signature)的问题

    原因:这种一般是因为项目依赖的某个jar包损坏引起的, 解决办法: 1.右键项目,选择maven,更新(update maven project) 2.通过右击项目名 ->  Run as -& ...

  4. File.Copy的时候Could not find a part of the path

    https://developercommunity.visualstudio.com/content/problem/378265/filecopy-did-not-throw-the-correc ...

  5. CSS animation-timing-function 属性中的 steps() 与 step-start,step-end

    steps() 设置间隔参数,可以实现分步过渡 第一个参数指定了时间函数中的间隔数量(必须是正整数)第二个参数可选,接受 start 和 end 两个值,指定在每个间隔的起点或是终点发生阶跃变化,默认 ...

  6. android.annotation.SuppressLint

    Lint是一个静态检查器,它围绕Android项目的正确性.安全性.性能.可用性以及可访问性进行分析.它检查的对象包括XML资源.位图.ProGuard配置文件.源文件甚至编译后的字节码. Lint包 ...

  7. LA-3029(扫描线)

    题意: 给定一个n*m的矩阵,一些格子是空地“F”,一些是障碍"R",找出一个全部由F组成的面积最大的子矩阵; 思路: 对每个格子维护up[i][j],le[i][j],ri[i] ...

  8. ekhtml使用总结

    ekhtml是一个高效SAX方式的HTML解析库. 文件说明 官网下载ekhtml-0.3.2.tar.gz文件解压后,内部包括源码.测试文件.文档.编译脚本等. 如需编译成静态库或动态库后进行集成, ...

  9. 开启sqlplus中执行计划

    在sqlplus中我们一般用Autotrace来查看执行计划,从而对于一些语句执行过程分析,开展优化工作.这里就演示一下如何将autotrace权限授予给普通的用户,以scott用户为例(set au ...

  10. this关键字使用

    原文地址:https://www.cnblogs.com/alsf/p/5515996.html 一,表示类中属性 1,没有使用this的情况 class Person{ // 定义Person类 p ...