Subset
Tags:
Medium
Skills:
backtrack
June 24, 2025
04:32 AM
No headings found
Loading content...
Related Posts
Leetcode
No headings found
Related Posts
Leetcode
Problem
Time and space complexity:
Solution - backtrack
1function subsets(nums: number[]): number[][] {
2 /**
3 backtrack
4 TC: O(2^n)
5 SC: O(2^n)
6 */
7 const res: number[][] = [];
8 function dfs(start: number, path: number[]): void {
9 res.push([...path]); // O(n)
10 for (let i = start; i < nums.length; i++) {
11 path.push(nums[i]);
12 dfs(i + 1, path);
13 path.pop()
14 }
15 }
16
17 dfs(0, [])
18 return res;
19};