Skip to content

Commit 48c9129

Browse files
committed
Subsets
1 parent 9a95063 commit 48c9129

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
| 0049 | Medium | Group Anagrams | Array, HashTable, String, Sorting | [solution](./docs/0049-Group-Anagrams.md ) |
2323
| 0074 | Medium | Search a 2D Matrix | Array, Binary Search, Matrix | [solution](./docs/0074-Search-A-2D-Matrix.md) |
2424
| 0076 | Hard | Minimum Window Substring | Hash Table, String, Sliding Window | [solution](./docs/0076-Mininum-Window-Substring.md) |
25+
| 0078 | Medium | Subsets | Array, Backtracking, Bit Manipulation | [solution](./docs/0078-Subsets.md) |
2526
| 0084 | Hard | Largest Rectangle in Histogram | Array, Stack, Monotonic Stack | [solution](./docs/0084-Largest-Rectangle-In-Histogram.md) |
2627
| 0098 | Medium | Validate Binary Search Tree | Tree, Depth-First Search, Binary Search Tree, Binary Tree | [solution](./docs/0098-Validate-Binary-Search-Tree.md) |
2728
| 0100 | Easy | Same Tree | Tree, Depth-First Search, Breadth-First Search, Binary Tree | [solution](./docs/0100-Same-Tree.md) |

docs/0078-Subsets.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# 🧠 [Subsets](https://leetcode.com/problems/subsets/description/)
2+
3+
## 💡 Intuition
4+
5+
To generate all possible subsets (the power set) of an array of unique integers, we recognize that each element has two
6+
choices: either it is included in a subset or it is not. This binary decision at each element leads to a total of
7+
\(2^n\) subsets for an array of length \(n\).
8+
9+
The key idea is to simulate this decision-making process iteratively using a queue (Breadth-First Search style), where
10+
each tuple in the queue represents a current subset and the index of the element being considered next.
11+
12+
## 🔍 Approach
13+
14+
- Use a `Deque` to perform a breadth-first construction of subsets.
15+
- Begin with an empty subset and index 0.
16+
- While the number of generated subsets is less than \(2^n\), pop a tuple off the queue.
17+
- For each tuple, branch into two new tuples:
18+
- One that keeps the subset as is (excluding the current index).
19+
- One that includes the current index element in the subset.
20+
- Push both new tuples back into the queue with the index incremented.
21+
- Continue until all subsets are generated.
22+
- Return only the subsets (not the indices).
23+
24+
## ⏱️ Complexity
25+
26+
- **Time Complexity**: \(O(2^n \cdot n)\)
27+
- There are \(2^n\) subsets.
28+
- Each subset can take up to \(O(n)\) time to copy or create.
29+
- **Space Complexity**: \(O(2^n \cdot n)\)
30+
- Due to storing all subsets in memory.
31+
32+
## 🧪 Code
33+
34+
- [Java](../src/main/java/io/dksifoua/leetcode/subsets/Solution.java)
35+
36+
## ✅ Summary
37+
38+
This solution takes an iterative BFS-inspired approach to subset generation. It systematically expands each subset by
39+
including or excluding the current element. The result is an efficient and clean method that produces all \(2^n\)
40+
subsets without duplication.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.dksifoua.leetcode.subsets;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
7+
import java.util.*;
8+
9+
public class Solution {
10+
11+
public List<List<Integer>> subsets(int[] nums) {
12+
Deque<Tuple> queue = new ArrayDeque<>() {{
13+
add(new Tuple(Collections.emptyList(), 0));
14+
}};
15+
int subsetLength = (int) Math.pow(2, nums.length);
16+
while (queue.size() < subsetLength) {
17+
Tuple tuple = queue.removeFirst();
18+
19+
20+
List<Integer> subset1 = new ArrayList<>(tuple.getSubset());
21+
List<Integer> subset2 = new ArrayList<>(tuple.getSubset()) {{ add(nums[tuple.getIndex()]); }};
22+
23+
queue.addLast(new Tuple(subset1, tuple.getIndex() + 1));
24+
queue.addLast(new Tuple(subset2, tuple.getIndex() + 1));
25+
}
26+
27+
return queue.stream().map(Tuple::getSubset).toList();
28+
}
29+
30+
@Getter
31+
@Setter
32+
@AllArgsConstructor
33+
private static class Tuple {
34+
private List<Integer> subset;
35+
private int index;
36+
}
37+
}

0 commit comments

Comments
 (0)