-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0090-subsets-ii.js
More file actions
39 lines (33 loc) · 934 Bytes
/
0090-subsets-ii.js
File metadata and controls
39 lines (33 loc) · 934 Bytes
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
/**
* Subsets II
* Time Complexity: O(N * 2^N)
* Space Complexity: O(N * 2^N)
*/
var subsetsWithDup = function (nums) {
const numbersSorted = nums
.slice()
.sort((valueOne, valueTwo) => valueOne - valueTwo);
const collectionOfSubsets = [];
function recursiveSearch(currentSubsetConstruction, beginIndex) {
collectionOfSubsets.push([...currentSubsetConstruction]);
for (
let iterationIndex = beginIndex;
iterationIndex < numbersSorted.length;
iterationIndex++
) {
if (
iterationIndex > beginIndex &&
numbersSorted[iterationIndex] === numbersSorted[iterationIndex - 1]
) {
continue;
}
const nextSubsetFormation = [
...currentSubsetConstruction,
numbersSorted[iterationIndex],
];
recursiveSearch(nextSubsetFormation, iterationIndex + 1);
}
}
recursiveSearch([], 0);
return collectionOfSubsets;
};