Skip to content

Commit bade561

Browse files
authored
Update 494-target-sum.js
1 parent 7db705c commit bade561

File tree

1 file changed

+5
-10
lines changed

1 file changed

+5
-10
lines changed

494-target-sum.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,25 @@
55
*/
66
const findTargetSumWays = function(nums, target) {
77
const sum = nums.reduce((a, b) => a+b);
8-
98
if(Math.abs(target) > sum) {
109
return 0;
1110
}
12-
1311
if((target + sum) % 2) {
1412
return 0;
1513
}
16-
17-
const halfSum = (target + sum) / 2;
18-
19-
let dp = new Array(halfSum+1).fill(0);
14+
const newTarget = (target + sum) / 2;
15+
let dp = new Array(newTarget+1).fill(0);
2016
dp[0] = 1;
21-
2217
for(let i = 0; i < nums.length; i++) {
23-
for(let j = halfSum; j >= nums[i]; j--) {
18+
for(let j = newTarget; j >= nums[i]; j--) {
2419
dp[j] += dp[j - nums[i]];
2520
}
2621
}
27-
28-
return dp[halfSum];
22+
return dp[newTarget];
2923
};
3024

3125

26+
3227
// another
3328

3429
/**

0 commit comments

Comments
 (0)