1
var canPartition = function (nums) {
2
let sum = nums.reduce((prevVal, currValue) => prevVal + currValue, 0); // sum of each values
3
if (sum % 2 !== 0) return false; // return false if odd sum
4

5
let target = sum / 2; // ex.[1,5,11,5] target is half which is 11
6
let dp = new Set(); // add unique values
7
dp.add(0); //intialize with 0
8
for (var i = nums.length - 1; i >= 0; i--) {
9
//start from end
10
nextDp = new Set();
11
for (const ele of dp.values()) {
12
let newVal = ele + nums[i];
13
if (newVal === target) return true;
14
nextDp.add(newVal);
15
}
16
dp = new Set([...dp, ...nextDp]);
17
}
18

19
return false;
20
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0