当前位置:网站首页>Brainstorm: Goals and
Brainstorm: Goals and
2022-08-10 15:25:00 【InfoQ】
title
Given an array of non-negative integers, a1, a2, ..., an, and a target number, S.Now you have two symbols + and -.For any integer in the array, you can choose a sign from + or - to prepend.
Returns the number of methods that can make the final array and all the symbols added to the target number S.
Example:
Input: nums: [1, 1, 1, 1, 1], S: 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 make the final goal sum to 3.
Tips:
- The array is not empty and the length will not exceed 20.
- The initial array sum will not exceed 1000.
- The final result returned is guaranteed to be stored as a 32-bit integer.
Solution ideas
Assuming that the sum of addition is x, then the sum corresponding to subtraction is sum - x.So what we require is x - (sum - x) = S, which can be deduced: x = (S + sum) / 2.
Therefore, this problem can be transformed into, filling capacity x knapsack, there are several ways.
If (S + sum) / 2 is not an integer, there is no solution at this time; and if the target number S is greater than sum, there is also no solution at this time.
This problem needs to be solved by filling the backpack. There are several methods in total, which is a combination problem.
The first step is to determine the dp array and the meaning of the subscript: p[j] means: there are dp[j] methods to fill up a bag with such a large volume of j.
The second step is to determine the recursion formula: the formula for finding a combination problem is generally dp[j] += dp[j - nums[i]].
The third step, dp array initialization: dp[0] must be initialized to 1 during initialization, because dp[0] is the origin of all recursive results in the formula, dp[0]0] = 1, which is also well explained in theory. There is one way to fill a backpack with a capacity of 0, which is to load 0 items.
The fourth step is to determine the traversal order: For the one-dimensional dp traversal of the 01 knapsack problem, nums is placed in the outer loop, target is in the inner loop, and the inner loop is in reverse order.
Code Implementation
class Solution {
public int findTargetSumWays(int[] nums, int target) {
int sum = 0;
for(int i = 0; i < nums.length; i++) sum += nums[i];
if ((target + sum) % 2 != 0) return 0;
int size = (target + sum) / 2;
if(size < 0) size = -size;
int[] dp = new int[size + 1];
dp[0] = 1;
for (int i = 0; i < nums.length; i++) {
for (int j=size; j >= nums[i]; j--) {
dp[j] += dp[j - nums[i]];
}
}
return dp[size];
}
}
Last
- Time complexity: O(n × m), n is a positive number, m is the backpack capacity
- Space complexity: O(m), m is the backpack capacity
边栏推荐
- PyTorch 多机多卡训练:DDP 实战与技巧
- 软件测试用例篇
- systemui屏蔽通知栏
- 易基因|深度综述:m6A RNA甲基化在大脑发育和疾病中的表观转录调控作用
- 社区动态——恭喜海豚调度中国区用户组新晋 9 枚“社群管理员”
- Meaning and names of 12 nautical miles, 24 nautical miles and 200 nautical miles
- Understanding_Data_Types_in_Go
- 领域驱动模型设计与微服务架构落地-从项目去剖析领域驱动
- Introduction to program debugging and its use
- Introduction to the functional logic of metaForce Fosage 2.0 system development
猜你喜欢
随机推荐
Yann LeCun转推:参数减少50倍,性能还更好,MetaAI推出Atlas信息检索模型
Zhaoqi Technology Innovation High-level Talent Entrepreneurship Competition Platform
电脑重装系统提示activex部件不能创建对象如何解决
老板加薪!看我做的WPF Loading!!!
关于async\await 的理解和思考
JS entry to proficient full version
华为云DevCloud获信通院首批云原生技术架构成熟度评估的最高级认证
How to code like a pro in 2022 and avoid If-Else
Data Types and Integer Storage
BCG库简介
Flask框架——MongoEngine使用MongoDB数据库
antd组件中a-modal设置固定高度,内容滚动显示
【数仓设计】企业数仓为什么要进行分层?(六大好处)
头脑风暴:目标和
Pytest framework optimization
容器化 | 在 S3 实现定时备份
解读STEAM教育中的表现性评价
systemui shield notification bar
容器化 | 在 S3 实现定时备份
数字藏品平台系统开发实战