Tensorum
YouTubeशिकायला सुरुवात करा
LeetCode 90 · Subsets

Subsets II

LeetCode 90 · मध्यम · उपाय, intuition आणि टप्प्याटप्प्याने वॉकथ्रू

Sort; skip nums[i] == nums[i−1] when i > index.

मध्यमArrayBacktrackingBit Manipulation

मी Subsets II (LeetCode 90) असा सोडवला — Subsets पॅटर्नमधील एक मध्यम प्रॉब्लेम. खाली: intuition, टप्प्याटप्प्याने approach, माझा accepted Python कोड, animated test cases आणि line-by-line debugger.

खालील विवरण इंग्रजीत आहे.

Intuition

Same as Subsets, but sort first so duplicates sit together. At each level, skip a value equal to the previous one at the same level — choosing it would recreate a subset you already built.

Approach

  1. 1

    Sort.

  2. 2

    Record the current subset.

  3. 3

    Loop i from index; skip duplicates at the same level.

  4. 4

    Append, recurse with i+1, pop.

मी कसा सोडवला

Backtracking with duplicate skipPythonO(n·2ⁿ) time
class Solution:
    def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
        nums.sort()
        res=[]
        def dfs(index,ds):
            res.append(ds[:])
            for i in range(index,len(nums)):
                if i!=index and nums[i]==nums[i-1]:
                    continue
                ds.append(nums[i])
                dfs(i+1,ds)
                ds.pop()
        dfs(0,[])
        return res
स्टेप-थ्रू लोड होत आहे…

Subsets II — वारंवार विचारले जाणारे प्रश्न

Subsets II मागील intuition काय आहे?

Same as Subsets, but sort first so duplicates sit together. At each level, skip a value equal to the previous one at the same level — choosing it would recreate a subset you already built.

Subsets II (LeetCode 90) कसा सोडवावा?

1. Sort. 2. Record the current subset. 3. Loop i from index; skip duplicates at the same level. 4. Append, recurse with i+1, pop.

Subsets II ची time आणि space complexity किती आहे?

Time: O(n · 2ⁿ). Space: O(n). (Accepted approach: Backtracking with duplicate skip.)

Subsets II मध्ये सामान्य चुका कोणत्या?

The condition is i != index (same level), not i > 0.