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.