Tensorum
YouTubeகற்கத் தொடங்குங்கள்
LeetCode 40 · Combinations

Combination Sum II

LeetCode 40 · நடுத்தரம் · தீர்வு, intuition & படிப்படியான வாக்த்ரூ

Sort; skip same-level duplicates; break on overshoot.

நடுத்தரம்ArrayBacktracking
LeetCode இல் திற ↗

நான் Combination Sum II (LeetCode 40) ஐ இப்படித் தீர்த்தேன் — Combinations பேட்டர்னின் ஒரு நடுத்தரம் பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.

கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.

Intuition

Each candidate can be used once, and the input has duplicates. Sort, recurse with i+1, and skip equal values at the same level. Because it's sorted, you can also break as soon as a candidate overshoots the target.

Approach

  1. 1

    Sort candidates.

  2. 2

    Equal total → record.

  3. 3

    Skip duplicates (i != start and same as previous).

  4. 4

    Break if candidate + total > target; else recurse with i+1.

நான் எப்படித் தீர்த்தேன்

Backtracking with pruningPythonO(2ⁿ) worst case
class Solution:
    def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
        res=[]
        candidates.sort()
        def dfs(start,ds,total):
            if total==target:
                res.append(ds[:])
                return 
            for i in range(start,len(candidates)):
                if i!=start and candidates[i]==candidates[i-1]:
                    continue
                if candidates[i]+total>target:
                    break
                ds.append(candidates[i])
                dfs(i+1,ds,total+candidates[i]) 
                ds.pop()
        dfs(0,[],0)
        return res
ஸ்டெப்-த்ரூ ஏற்றுகிறது…

Combination Sum II — அடிக்கடி கேட்கப்படும் கேள்விகள்

Combination Sum II பின்னாலுள்ள intuition என்ன?

Each candidate can be used once, and the input has duplicates. Sort, recurse with i+1, and skip equal values at the same level. Because it's sorted, you can also break as soon as a candidate overshoots the target.

Combination Sum II (LeetCode 40) ஐ எப்படித் தீர்ப்பது?

1. Sort candidates. 2. Equal total → record. 3. Skip duplicates (i != start and same as previous). 4. Break if candidate + total > target; else recurse with i+1.

Combination Sum II இன் time மற்றும் space complexity என்ன?

Time: O(2ⁿ). Space: O(n). (Accepted approach: Backtracking with pruning.)

Combination Sum II இல் பொதுவான தவறுகள் என்ன?

Break, don't continue — every later candidate is even bigger.