leetcode 494. Target Sum 深度优先遍历DFS + DP动态规划
来源:互联网 发布:天下三男鬼墨捏脸数据 编辑:程序博客网 时间:2024/05/18 23:57
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.
这道题最直接的方法就是DFS深度优先遍历,不过可能超时,但是还有一个很棒的DP做法,我是参考网上的分析,主要分析如下:
1、该问题求解数组中数字只和等于目标值的方案个数,每个数字的符号可以为正或负(减整数等于加负数)。
2、该问题和矩阵链乘很相似,是典型的动态规划问题
3、举例说明: nums = {1,2,3,4,5}, target=3, 一种可行的方案是+1-2+3-4+5 =3
该方案中数组元素可以分为两组,一组是数字符号为正(P={1,3,5}),另一组数字符号为负(N={2,4})
因此: sum(1,3,5) - sum(2,4) = target
sum(1,3,5) - sum(2,4) + sum(1,3,5) + sum(2,4) = target + sum(1,3,5) + sum(2,4)
2sum(1,3,5) = target + sum(1,3,5) + sum(2,4)
2sum(P) = target + sum(nums)
sum(P) = (target + sum(nums)) / 2
由于target和sum(nums)是固定值,因此原始问题转化为求解nums中子集的和等于sum(P)的方案个数问题
4、求解nums中子集合只和为sum(P)的方案个数(nums中所有元素都是非负)
该问题可以通过动态规划算法求解
举例说明:给定集合nums={1,2,3,4,5}, 求解子集,使子集中元素之和等于9 = new_target = sum(P) = (target+sum(nums))/2
定义dp[10]数组, dp[10] = {1,0,0,0,0,0,0,0,0,0}
dp[i]表示子集合元素之和等于当前目标值的方案个数, 当前目标值等于9减去当前元素值
当前元素等于1时,dp[9] = dp[9] + dp[9-1]
dp[8] = dp[8] + dp[8-1]
…
dp[1] = dp[1] + dp[1-1]
当前元素等于2时,dp[9] = dp[9] + dp[9-2]
dp[8] = dp[8] + dp[8-2]
…
dp[2] = dp[2] + dp[2-2]
当前元素等于3时,dp[9] = dp[9] + dp[9-3]
dp[8] = dp[8] + dp[8-3]
…
dp[3] = dp[3] + dp[3-3]
当前元素等于4时,
…
当前元素等于5时,
…
dp[5] = dp[5] + dp[5-5]
最后返回dp[9]即是所求的解
代码如下:
#include <iostream>#include <vector>#include <map>#include <set>#include <queue>#include <stack>#include <string>#include <climits>#include <algorithm>#include <sstream>#include <functional>#include <bitset>#include <cmath>using namespace std;class Solution{public: int findTargetSumWays(vector<int>& nums, int target) { if (nums.size() <= 0) return 0; int sum = 0; for (int a : nums) sum += a; if (sum < target || (sum+target)%2 == 1) return 0; int count = subSetSum(nums,(sum+target)/2); return count; } int subSetSum(vector<int> a, int sum) { vector<int> dp(sum + 1,0); dp[0] = 1; for (int i = 0; i < a.size(); i++) { for (int j = sum; j >= a[i]; j--) { dp[j] += dp[j - a[i]]; } } return dp[sum]; } int findTargetSumWaysByDFS(vector<int>& nums, int S) { if (nums.size() <= 0) return 0; int count = 0; getAll(nums, 0, 0, S, count); return count; } void getAll(vector<int> a, int index, int sum,int target, int& count) { if (index == a.size()) { if (sum == target) count++; return; } else { getAll(a, index + 1, sum + a[index], target, count); getAll(a, index + 1, sum - a[index], target, count); } }};
- leetcode 494. Target Sum 深度优先遍历DFS + DP动态规划
- leetcode 377. Combination Sum IV 组合之和 + DP动态规划 + DFS深度优先遍历
- leetcode 416. Partition Equal Subset Sum 动态规划DP + DFS深度优先遍历
- leetcode 91. Decode Ways DP动态规划+DFS深度优先遍历
- leetcode 140. Word Break II 动态规划DP + DFS深度优先搜索
- leetcode 112. Path Sum DFS深度优先遍历
- leetcode 113. Path Sum II DFS深度优先遍历
- leetcode 404. Sum of Left Leaves DFS深度优先遍历
- leetcode 437. Path Sum III 深度优先遍历DFS
- HDU1011Starship Troopers(DP+DFS)深度优先搜索+动态规划
- leetcode 337. House Robber III DP动态规划 + DFS深度有限遍历
- 动态规划-494. Target Sum
- leetcode -- 494. Target Sum【数学转化 + 动态规划】
- 动态规划编程实例——LeetCode 494.Target Sum
- LeetCode 之 DFS 深度优先遍历
- leetcode 508. Most Frequent Subtree Sum 一个简单的DFS深度优先遍历的做法
- leetcode 97. Interleaving String(字符串交错出现) DFS深度优先遍历+DP
- 深度优先遍历DFS
- RabbitMQ学习心得——发布/订阅(上)
- 日期常用类的整理
- Windows共享内存
- ant design表格添加loading效果
- CodeForces gym101173 Bipartite Blanket
- leetcode 494. Target Sum 深度优先遍历DFS + DP动态规划
- Elasticsearch6.x之Logstash安装-yellowcong
- 彻底理解Linux的各种终端类型以及概念
- SpringMVC返回json数据
- 关于Pandas中的部分索引技巧
- iOS中的常见事件及其响应
- STl训练每个题的总结
- 数据结构实验之查找二:平衡二叉树
- docker 4 安装