-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path416-partition-equal-subset-sum.js
More file actions
61 lines (50 loc) · 1.33 KB
/
Copy path416-partition-equal-subset-sum.js
File metadata and controls
61 lines (50 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
var canPartition = function(nums) {
let totalSum = nums.reduce((sum, num) => sum+ num, 0);
if (totalSum % 2 !== 0) {
return false;
}
const result = canPartitionBottomTop(nums, totalSum / 2);
return result;
};
var canPartitionBottomTop = function(nums, target) {
let dp = new Set([0]);
for (let i = nums.length - 1; i >= 0; i--) {
const nextDp = new Set();
const num = nums[i];
for(const j of dp) {
const sum = num + j;
if (sum === target) return true;
nextDp.add(j);
nextDp.add(sum);
}
dp = nextDp;
}
return false;
};
var canPartitionTopBottom = function(nums, target) {
const dp = {};
const dfs = (i, amt) => {
if (!dp[i]) dp[i] = {};
if (i === nums.length || amt === target) {
return amt === target;
}
const num = nums[i],
useThisValue = dfs(i + 1, amt + num),
skipThisValue = dfs(i + 1, amt);
dp[i][amt] = useThisValue || skipThisValue;
return dp[i][amt];
};
const result = dfs(0, 0);
return result;
}
const data = [
{ nums: [1,5,11,5], output: true },
{ nums: [1,2,3,5], output: false }
];
for (let d of data) {
console.log(JSON.stringify(d));
const result = canPartition(d.nums);
console.log('result = ', result);
console.log('ok = ', result === d.output);
console.log('----------');
}