-
Notifications
You must be signed in to change notification settings - Fork 157
/
Copy pathpartition-equal-subset-sum.js
104 lines (87 loc) · 2 KB
/
partition-equal-subset-sum.js
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
* Partition Equal Subset Sum
*
* Given a non-empty array containing only positive integers, find if the array can be partitioned
* into two subsets such that the sum of elements in both subsets is equal.
*
* Note:
* Each of the array element will not exceed 100.
* The array size will not exceed 200.
*
* Example 1:
*
* Input: [1, 5, 11, 5]
* Output: true
*
* Explanation: The array can be partitioned as [1, 5, 5] and [11].
*
* Example 2:
*
* Input: [1, 2, 3, 5]
* Output: false
*
* Explanation: The array cannot be partitioned into equal sum subsets.
*/
/**
* Solution I - O(n^2) space
*
* @param {number[]} nums
* @return {boolean}
*/
const canPartition = nums => {
// Step 1. calculate the sum and make a sanity check
let sum = nums.reduce((total, num) => total + num);
if ((sum & 1) === 1) {
return false;
}
sum /= 2;
// Step 2. Initialize the dp table
const n = nums.length;
const dp = Array(n + 1);
for (let i = 0; i < dp.length; i++) {
dp[i] = Array(sum + 1).fill(false);
}
dp[0][0] = true;
for (let i = 1; i < n + 1; i++) {
dp[i][0] = true;
}
for (let j = 1; j < sum + 1; j++) {
dp[0][j] = false;
}
for (let i = 1; i < n + 1; i++) {
for (let j = 1; j < sum + 1; j++) {
dp[i][j] = dp[i - 1][j];
if (j >= nums[i - 1]) {
dp[i][j] = dp[i][j] || dp[i - 1][j - nums[i - 1]];
}
}
}
return dp[n][sum];
};
/**
* Solution II - O(n) space
*
* @param {number[]} nums
* @return {boolean}
*/
const canPartitionII = nums => {
// Step 1. calculate the sum and make a sanity check
let sum = nums.reduce((total, num) => total + num);
if ((sum & 1) === 1) {
return false;
}
sum /= 2;
// Step 2. Initialize the dp table
const n = nums.length;
const dp = Array(sum + 1).fill(false);
dp[0] = true;
for (let num of nums) {
for (let i = sum; i > 0; i--) {
if (i >= num) {
dp[i] = dp[i] || dp[i - num];
}
}
}
return dp[sum];
};
export { canPartition, canPartitionII };