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. linux服务器性能(网卡流量、CPU、内存、磁盘使用率)监控

    广义的网站的监控涵盖所有的非业务行为的数据采集与管理,包括数据分析师和产品设计师使用的网站用户行为日志.业务运行数据,以及供运维工程师和开发工程师使用的性能统计数据等. 本文主要是通过shell脚本来 ...

  2. python object takes no parameters

    class Song(object): def __init__(self,lyrics): self.lyrics = lyrics def sing_me_a_song(self): for li ...

  3. Activity设置全屏显示的两种方式及系统自带theme属性解析

    转载说明:原贴地址:http://blog.csdn.net/a_running_wolf/article/details/50480386 设置Activity隐藏标题栏.设置Activity全屏显 ...

  4. 关于Yii框架的基础知识

    第一次写博文,也不知道怎么写,不太熟悉,带小伙伴学习一样我日常使用的Yii框架. PHP中的开发框架有很多,比如:ThinkPHP.Yii.CI.Laravel.Phalcon等.现在流行度最高的是L ...

  5. 微信公众号开发笔记3-sdk接入(nodejs)

    另一个2小时 access_token是需要2小时更新一次,在这里,又引入了一个2小时获取一次的字段,这个字段是: jsapi_ticket,这个字段是接入sdk的前提.与access_token类似 ...

  6. Oracle的instr函数

    instr函数 instr(目标字符串,被匹配的字符串,搜索的开始位置默认是1,第几次被搜索到) 例子1: SQL> select ename,instr(ename,'L',1,1) from ...

  7. SYRefresh 一款简洁易用的刷新控件 支持tableview,collectionview水平垂直刷新功能

    SYRefresh 地址: https://github.com/shushaoyong/SYRefresh 一款简洁易用的刷新控件 示例程序:   默认刷新控件使用方法: //添加头部刷新控件 Sc ...

  8. ng-checked选择和点击增加dom

      1.需求 在添加页面实现一个checkbox的选择,然后在详情页面展示时,会自动选上之前被选中的. 2.添加页面 看官最好将这个代码复制过去看看效果. <!DOCTYPE html>& ...

  9. Eclipse中启动tomcat时内存溢出

    今天在启动自己项目的时候遇到一个永久带(permgen space)内存溢出,查找了很多资料和请教了许多大神,最终才解决问题. 一.什么原因造成了永久带溢出: 1.项目使用了太多的静态变量 2.加载了 ...

  10. iOS自动打包并发布脚本

    假如你的项目目录如下所示: |____AOP | |____AppDelegate.h | |____AppDelegate.m | |____Base.lproj | | |____LaunchSc ...