Combination Sum
LeetCode 39 · நடுத்தரம் · தீர்வு, intuition & படிப்படியான வாக்த்ரூ
Recurse with the same index to allow reuse.
நான் Combination Sum (LeetCode 39) ஐ இப்படித் தீர்த்தேன் — Combinations பேட்டர்னின் ஒரு நடுத்தரம் பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.
கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.
Intuition
Each candidate can be reused, so after choosing candidates[i] you recurse starting from i again (not i+1). Stop when the running total hits or passes the target.
Approach
- 1
dfs(start, ds, total): over target → stop; equal → record.
- 2
For i from start: append, dfs(i, …), pop.
நான் எப்படித் தீர்த்தேன்
Backtracking with reusePythonexponential
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
res=[]
def dfs(start,ds,total):
if total>target:
return
if total==target:
res.append(ds[:])
return
for i in range(start,len(candidates)):
ds.append(candidates[i])
dfs(i,ds,total+candidates[i]) #take duplicate
ds.pop()
dfs(0,[],0)
return resCombination Sum — அடிக்கடி கேட்கப்படும் கேள்விகள்
Combination Sum பின்னாலுள்ள intuition என்ன?
Each candidate can be reused, so after choosing candidates[i] you recurse starting from i again (not i+1). Stop when the running total hits or passes the target.
Combination Sum (LeetCode 39) ஐ எப்படித் தீர்ப்பது?
1. dfs(start, ds, total): over target → stop; equal → record. 2. For i from start: append, dfs(i, …), pop.
Combination Sum இன் time மற்றும் space complexity என்ன?
Time: O(2^(T/min)). Space: O(T/min). (Accepted approach: Backtracking with reuse.)
Combination Sum இல் பொதுவான தவறுகள் என்ன?
Starting the loop at start (not 0) prevents permutations of the same combination.