SDE & MLE Prep

78. Subsets

Problem

nums holds distinct integers. Return every subset (the power set), in any order, with no duplicate subsets.

Example: [1,2,3] → [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]

Constraints: n ≤ 10.

Every node of the decision tree is a subset — record the path on entry.

Complexity: O(n · 2ⁿ).

class Solution:
    def subsets(self, nums: List[int]) -> List[List[int]]:
        res, path = [], []

        def bt(start):
            res.append(path[:])
            for i in range(start, len(nums)):
                path.append(nums[i])
                bt(i + 1)
                path.pop()

        bt(0)
        return res