Subsets
medium 22 minGenerating every subset is the first backtracking pattern most interviews ask for, and there are four standard ways to write it: take-or-skip recursion, a start-index loop, iterative cascading and bitmask counting.
Check yourself
One question at a time. Each answer explains why.
Question 1 of 10
What is the time complexity of generating all subsets of n elements?
Why does the subsets loop use a start index when permutations do not?
How many subsets does a set of 4 elements have?
In the start-index template, what happens at the start of every call, before exploring further?
With the bitmask method on {A, B, C}, which subset does binary 101 stand for?
In take-or-skip backtracking, what does the pop between the two recursive calls do?
Why is the empty set always part of the answer?
Subsets II on [1, 2, 2]: how many subsets does the correct code return?
A Subsets II solution uses `if i > 0 and nums[i] == nums[i - 1]: continue`. What happens on sorted [1, 2, 2]?
Cascading on [1, 2, 3]: what does result hold after the first two elements are processed?
Pick an answer to see why.
Not quite. There are 2ⁿ subsets and each takes up to O(n) to build or copy into the result.
Not quite. Only adding later elements fixes one build order per subset, so [2, 1] is never built after [1, 2]. Permutations need every unused element at every position.
Not quite. 2⁴ = 16. The 24 is 4!, the number of permutations.
Not quite. Every node of the start-index tree is an answer, so it is saved on arrival. Saving only at a target length would give combinations of that size instead.
Not quite. Bit 0 (rightmost) is A and bit 2 is C. 101 has bits 0 and 2 on.
Not quite. After exploring with nums[i] taken, pop removes it and the second call explores the skip branch. In the start-index version the pop likewise undoes one choice before the loop tries the next element.
Not quite. Leaving every element out is one of the 2ⁿ in-or-out choices: the all-skip leaf, the root call in the start-index version, mask 0. Leaving it out gives 2ⁿ - 1 answers and fails the tests.
Not quite. [], [1], [1,2], [1,2,2], [2], [2,2]: choose 0 or 1 copy of 1 and 0, 1 or 2 copies of 2, so 2 × 3 = 6. The plain algorithm gives 8 with two repeats.
Not quite. The second 2 is blocked even when it is the first option one level deeper (i == start), so no subset can hold both 2s. The rule must be i > start.
Not quite. Start [[]]; adding 1 to each gives [[], [1]]; adding 2 to each of those gives [[], [1], [2], [1, 2]]. Four subsets, the full power set of [1, 2].
Flashcards
Say your answer, then reveal it.
Card 1 of 12
How many subsets do {1, 2, 3} and a 4-element set have?
2³ = 8 and 2⁴ = 16. Each element is in or out, and the empty and full sets both count.
Say your answer, then reveal it.
What does the start-index loop do at the top of every call?
Saves a copy of the current path: every node of its tree is a valid subset, the empty one included.
Say your answer, then reveal it.
Take-or-skip vs start-index: where are the answers?
Take-or-skip: only at the 2ⁿ leaves (2ⁿ⁺¹ - 1 calls). Start-index: at every node (2ⁿ calls).
Say your answer, then reveal it.
Write the cascading version in one line.
Start with result = [[]], then for each x: result += [s + [x] for s in result]. The list doubles each round.
Say your answer, then reveal it.
Bitmask loop for all subsets?
for mask in range(1 << n), and take nums[i] when mask >> i & 1. Mask 0 is the empty set, 2ⁿ - 1 the full set.
Say your answer, then reveal it.
Check, add, remove and toggle element i in a mask?
Check mask & (1 << i); add mask | (1 << i); remove mask & ~(1 << i); toggle mask ^ (1 << i).
Say your answer, then reveal it.
Subsets II: the two changes to plain subsets?
Sort the input, and in the loop skip nums[i] when i > start and nums[i] == nums[i - 1].
Say your answer, then reveal it.
How many distinct subsets does a list with repeats have?
The product of (count + 1) over the distinct values: [1, 2, 2] has 2 × 3 = 6.
Say your answer, then reveal it.
Subsets II in take-or-skip form: what changes?
Taking is the same, but skipping jumps past every copy of the value at once.
Say your answer, then reveal it.
What does FAIS stand for?
Forward only (start index), Add every path, Include the element (append), Subtract it after (pop).
Say your answer, then reveal it.
Which LeetCode problems are the subsets classics?
78 Subsets, 90 Subsets II, then 77 Combinations, 39 and 40 Combination Sum, 416 and 698 partition problems.
Say your answer, then reveal it.
Why is O(n · 2ⁿ) the best possible for listing subsets?
The subsets hold n · 2ⁿ⁻¹ elements in total, so just writing the output takes that long.
Say your answer, then reveal it.
These cards come back in Today, just before you would forget them.