-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path416_partition-equal-subset-sum.js
211 lines (201 loc) · 5.43 KB
/
416_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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/**
*
* Problem:
* Given a set of positive integer numbers, find if we can partition it into two subsets
* such that the sum of elements in both subsets is equal.
* https://leetcode.com/problems/partition-equal-subset-sum/
*
* Example 1:
* Input: [1, 2, 3, 4]
* Output: true
* Explanation: The given set can be partitioned into two subsets with equal sum: [1, 4] &
* [2, 3]
*
* Example 2:
* Input: [1, 1, 3, 4, 7]
* Output: true
* Explanation: The given set can be partitioned into two subsets with equal sum:
* [1, 3, 4] & [1, 7]
*
*
* Note: the problem can be transformed to: given a number array, check if we can find a
* subset whose sum will be half of the total sum of the array. Obviously, if the total
* sum is not even number, we can return false directly, cause the array only contains
* integer.
*/
/**
*
* Solution 1: recursive brute force.
*
* @param {number[]} nums
* @return {boolean}
*/
function canPartitionToEqualSum1(nums) {
let totalSum = 0;
for (let i = 0; i < nums.length; i++) {
totalSum += nums[i];
}
// return false directly if the total sum is not even
if (totalSum % 2) {
return false;
}
return _recursive(nums, totalSum / 2, 0);
}
/**
*
* Time: O(2^n) <- for each number, we can choose to select or skip, 2 options.
* Space: O(nm) m: targetSum <- for recursion stack
*
* @param {*} nums
* @param {*} targetSum
* @param {*} currentIndex
* @return {boolean}
*/
function _recursive(nums, targetSum, currentIndex) {
/**
* Exit condition:
* * return true directly if we got 0 target sum, cause the half sum subset is found
* * return false when either there's no remaining numbers or the sum of the existing
* subset is already larger than the target sum.
*/
if (targetSum === 0) {
return true;
}
if (currentIndex >= nums.length || targetSum < 0) {
return false;
}
/**
* For each number, we can choose to select it or skip it, with this in mind,
* we call the `_recursive` for these 2 options, we return `true` immediately
* if the 1st options is viable.
*/
const selectCurrent = _recursive(
nums,
targetSum - nums[currentIndex],
currentIndex + 1
);
if (selectCurrent) {
return true;
}
const skipCurrent = _recursive(nums, targetSum, currentIndex + 1);
return skipCurrent;
}
/**
*
* Solution 2: top-down dynamic programming with memoization.
*
* @param {number[]} nums
* @return {boolean}
*/
function canPartitionToEqualSum2(nums) {
let totalSum = 0;
for (let i = 0; i < nums.length; i++) {
totalSum += nums[i];
}
// return false directly if the total sum is not even
if (totalSum % 2) {
return false;
}
const memo = new Array(nums.length)
.fill(0)
.map(() => new Array(totalSum / 2 + 1));
return _recursiveWithMemo(nums, totalSum / 2, 0, memo);
}
/**
*
* Time: O(mn) m: target sum
* Space: O(mn) <- for `memo` and recursion stack
*
* @param {*} nums
* @param {*} targetSum
* @param {*} currentIndex
* @param {number[][]} memo
* @return {boolean}
*/
function _recursiveWithMemo(nums, targetSum, currentIndex, memo) {
if (typeof memo[currentIndex][targetSum] === 'undefined') {
memo[currentIndex][targetSum] = _recursive(nums, targetSum, currentIndex);
}
return memo[currentIndex][targetSum];
}
/**
*
* Solution 3: bottom-up dynamic programming.
*
* Time: O(mn) m: target sum <- loop to populate `dp`
* Space: O(mn) <- for the `dp`
*
* @param {number[]} nums
* @return {boolean}
*/
function canPartitionToEqualSum3(nums) {
const n = nums.length;
let totalSum = 0;
for (let i = 0; i < n; i++) {
totalSum += nums[i];
}
// return false directly if the total sum is not even
if (totalSum % 2) {
return false;
}
const targetSum = totalSum / 2;
const dp = new Array(n + 1)
.fill(0)
.map(() => new Array(targetSum + 1).fill(false));
dp[0][0] = true;
for (let i = 1; i <= n; i++) {
for (let j = 0; j <= targetSum; j++) {
if (j >= nums[i - 1]) {
dp[i][j] = dp[i - 1][j] || dp[i - 1][j - nums[i - 1]];
} else {
dp[i][j] = dp[i - 1][j];
}
}
// early return: if we find a `targetSum` column equal to true, return directly
if (dp[i][targetSum]) {
return true;
}
}
return dp[n][targetSum];
}
/**
*
* Solution 4: bottom-up dynamic programming with reduced space.
*
* Time: O(mn) m: target sum <- loop to populate `dp`
* Space: O(m) <- for the `dp`
*
* @param {number[]} nums
* @return {boolean}
*/
function canPartitionToEqualSum4(nums) {
const n = nums.length;
let totalSum = 0;
for (let i = 0; i < n; i++) {
totalSum += nums[i];
}
// return false directly if the total sum is not even
if (totalSum % 2) {
return false;
}
const targetSum = totalSum / 2;
const dp = new Array(targetSum + 1).fill(false);
dp[0] = true;
for (let i = 1; i <= n; i++) {
for (let j = targetSum; j >= nums[i - 1]; j--) {
dp[j] = dp[j] || dp[j - nums[i - 1]];
}
// early return: if we find a `targetSum` column equal to true, return directly
if (dp[targetSum]) {
return true;
}
}
return dp[targetSum];
}
// Test
console.log(canPartitionToEqualSum1([1, 2, 3, 4])); // true
console.log(canPartitionToEqualSum1([1, 1, 3, 4, 7])); // true
console.log(canPartitionToEqualSum1([2, 3, 4, 6])); // false
console.log(canPartitionToEqualSum2([1, 1, 3, 4, 7])); // true
console.log(canPartitionToEqualSum3([1, 1, 3, 4, 7])); // true
console.log(canPartitionToEqualSum4([1, 1, 3, 4, 7])); // true