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.
Solution:
 
1st use  DFS recursive,  like a binary tree;   Go to left (+1) or right (-1); then  when sum is S  go the upper next。
But it is TLE in python

 

     count = 0
def findTargetSumWays(self, nums, S):
def findTargetHelper(nums, index, S):
if index == len(nums):
if S == 0:
self.count = self.count + 1
return findTargetHelper(nums, index+1, S - nums[index]) #+1
findTargetHelper(nums, index+1, S + nums[index]) #-1 findTargetHelper(nums, 0, S) return self.count
2.   DP

Refer to the other's idea that I didn't came up with .   
this problem could be transferred to subset sum problem (similar to knapback problem)
if there exist a solution,
P: positive number subset;     N: positive number subset
 then sum(P) - sum(N)  = S;               |P| + |N| = len(nums)
so  sum(P) + sum(N) + sum(P) - sum(N) =  S + sum(P) + sum(N)
         2*sum(P) = S + sum(nums)
            sum(P) = (S + sum(nums))/2
 the original problem  has been changed to a subset sum problem: :
Find a subset P of nums such that sum(P) = (target + sum(nums)) / 2
 
         def subsetSum(nums, target):
dp = [0]*(target+1)
dp[0] = 1
for num in nums:
for j in range(target, num-1, -1):
dp[j] += dp[j-num]
#print ("dp: ", target, j, dp[j])
return dp[target] sumN = sum(nums) if sumN < S or (S+sumN) %2 != 0:
return 0
return subsetSum(nums, (S+sumN)>>1)

[Leetcode] DP -- 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. 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 ...

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

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

  4. [LeetCode] Target Sum 目标和

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

  5. LeetCode Target Sum

    原题链接在这里:https://leetcode.com/problems/target-sum/description/ 题目: You are given a list of non-negati ...

  6. Leetcode之深度优先搜索(DFS)专题-494. 目标和(Target Sum)

    Leetcode之深度优先搜索(DFS)专题-494. 目标和(Target Sum) 深度优先搜索的解题详细介绍,点击 给定一个非负整数数组,a1, a2, ..., an, 和一个目标数,S.现在 ...

  7. 494. Target Sum - Unsolved

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

  8. Leetcode 之 Combination Sum系列

    39. Combination Sum 1.Problem Find all possible combinations of k numbers that add up to a number n, ...

  9. [LeetCode] 377. Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

随机推荐

  1. Unity SLua 如何调用Unity中C#方法

    1.原理 就是通常在Lua框架中所说的,开放一个C#的web接口,或者叫做在Slua框架中注册函数. 2.作用 在Lua中调用C#中的方法,这个是在做热更新中很常用的一种方法,无论是slua,还是lu ...

  2. webstorm安装与本地激活

    webstorm下载及安装 官方下载地址如下:https://www.jetbrains.com/webstorm/ 安装: 直接双击安装,注意路径中不要出现中文. 激活:(此方法来自网络) 许多人j ...

  3. JSP页面的静态包含和动态包含

    JSP中有两种包含:静态包含:<%@include file="被包含页面"%>和动态包含:<jsp:include page="被包含页面" ...

  4. 分布式服务:Dubbo+Zookeeper+Proxy+Restful 分布式架构

    分布式 分布式服务:Dubbo+Zookeeper+Proxy+Restful 分布式消息中间件:KafKa+Flume+Zookeeper 分布式缓存:Redis    分布式文件:FastDFS ...

  5. linux统计多个文件大小总和

    首先:查看当前文件夹大小的命令是: [root@host1 test]# du -sh 39M . 查看当前文件夹下所有文件的大小: [root@host1 test]# du -sh * 108K ...

  6. jQuery插件ImgAreaSelect 实例讲解二

    在上一篇随笔http://www.cnblogs.com/chenguanai/p/6883401.html中,已经了解了头像的上传预览和裁剪功能:那么这次就再看一下imgareaselect的裁剪功 ...

  7. Java Final and Immutable

    1. Final keyword Once a variable X is defined final, you can't change the reference of X to another ...

  8. JAVA网络编程TCP通信

    Socket简介: Socket称为"套接字",描述IP地址和端口.在Internet上的主机一般运行多个服务软件,同时提供几种服务,每种服务都打开一个Socket,并绑定在一个端 ...

  9. Lock(一)认识v$LOCK

    v$lock列出了数据库当前拥有的锁及未完成的锁请求. Column Description ADDR 被锁对象的地址 KADDR 锁的地址 SID session id(这里特指正在锁定对象或请求去 ...

  10. C语言之变量和数据类型

    常量:程序在运行过程中无法对值进行更改. 变量:是在计算机内存空间一种表示,声明变量将会向计算机内存申请存储空间,用于保存数据,计算机的CPU会从内存中加载数据. 声明变量: 数据类型  变量名[=值 ...