Tensorum
YouTubeనేర్చుకోవడం ప్రారంభించండి
LeetCode 90 · Subsets

Subsets II

LeetCode 90 · మధ్యస్థం · పరిష్కారం, intuition & దశలవారీ వాక్‌త్రూ

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

మధ్యస్థంArrayBacktrackingBit Manipulation
LeetCode లో తెరవండి ↗

నేను 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.