We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 7db705c commit bade561Copy full SHA for bade561
494-target-sum.js
@@ -5,30 +5,25 @@
5
*/
6
const findTargetSumWays = function(nums, target) {
7
const sum = nums.reduce((a, b) => a+b);
8
-
9
if(Math.abs(target) > sum) {
10
return 0;
11
}
12
13
if((target + sum) % 2) {
14
15
16
17
- const halfSum = (target + sum) / 2;
18
19
- let dp = new Array(halfSum+1).fill(0);
+ const newTarget = (target + sum) / 2;
+ let dp = new Array(newTarget+1).fill(0);
20
dp[0] = 1;
21
22
for(let i = 0; i < nums.length; i++) {
23
- for(let j = halfSum; j >= nums[i]; j--) {
+ for(let j = newTarget; j >= nums[i]; j--) {
24
dp[j] += dp[j - nums[i]];
25
26
27
28
- return dp[halfSum];
+ return dp[newTarget];
29
};
30
31
+
32
// another
33
34
/**
0 commit comments