Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 535afe6

Browse files
subset/powerset problem
1 parent c7cb9fd commit 535afe6

File tree

4 files changed

+107
-0
lines changed

4 files changed

+107
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
*
3+
* @param {*} nums
4+
*/
5+
function subsetBacktrack(nums, start = 0, current = [], result = []) {
6+
// console.log({ nums, start, current, result });
7+
result.push([...current]);
8+
9+
for (let i = start; i < nums.length; i++) {
10+
current.push(nums[i]);
11+
subsetBacktrack(nums, i + 1, current, result);
12+
current.pop();
13+
// console.log(current.pop());
14+
}
15+
return result;
16+
}
17+
18+
module.exports = subsetBacktrack;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Bitmask approach
3+
* @param {number[]} nums
4+
* @return {number[][]}
5+
*/
6+
function subsetsBinary(nums) {
7+
const len = nums.length;
8+
const max = 2 ** len;
9+
const result = [];
10+
11+
for (let bin = 0; bin < max; bin++) {
12+
const bitmask = bin.toString(2).padStart(len, '0');
13+
14+
const el = Array.from(bitmask).reduce((a, bit, i) => {
15+
if (bit === '1') {
16+
return a.concat(nums[i]);
17+
}
18+
return a;
19+
}, []);
20+
21+
result.push(el);
22+
}
23+
24+
return result;
25+
}
26+
27+
module.exports = subsetsBinary;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
*
3+
* https://leetcode.com/problems/subsets
4+
* @param {number[]} nums
5+
* @return {number[][]}
6+
*/
7+
function subsetsConcat(nums) {
8+
return nums.reduce((result, n) => {
9+
const newSet = result.map(r => r.concat(n));
10+
return result.concat(newSet); // O(n * n^2) bcuz copy
11+
}, [
12+
[],
13+
]);
14+
}
15+
16+
module.exports = subsetsConcat;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// npx jest lab/exercises/01-arrays/powerset/powerset.spec.js --watch
2+
3+
const backtrack = require('./powerset-backtrack');
4+
const subsetsBinary = require('./powerset-binaryNumbers');
5+
const subsetsConcat = require('./powerset-concat');
6+
7+
const approaches = [backtrack, subsetsBinary, subsetsConcat];
8+
9+
approaches.forEach((approach) => {
10+
describe(`Powerset ${approach.name}`, () => {
11+
it('should work with 0 elements', () => {
12+
expect(approach([])).toEqual([
13+
[],
14+
]);
15+
});
16+
17+
it('should work with 1 element', () => {
18+
expect(approach([1])).toEqual([
19+
[],
20+
[1],
21+
]);
22+
});
23+
24+
it('should work with 2 elements', () => {
25+
expect(approach([1, 2])).toEqual(expect.arrayContaining([
26+
[],
27+
[1],
28+
[2],
29+
[1, 2],
30+
]));
31+
});
32+
33+
it('should work with 3 elements', () => {
34+
expect(approach([1, 2, 3])).toEqual(expect.arrayContaining([
35+
[],
36+
[1],
37+
[2],
38+
[1, 2],
39+
[3],
40+
[2, 3],
41+
[1, 3],
42+
[1, 2, 3],
43+
]));
44+
});
45+
});
46+
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /