Tensorum
YouTubeশেখা শুরু করুন
LeetCode 39 · Combinations

Combination Sum

LeetCode 39 · মাঝারি · সমাধান, intuition ও ধাপে ধাপে ওয়াকথ্রু

Recurse with the same index to allow reuse.

মাঝারিArrayBacktracking
LeetCode-এ খুলুন ↗

আমি 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. 1

    dfs(start, ds, total): over target → stop; equal → record.

  2. 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 res
স্টেপ-থ্রু লোড হচ্ছে…

Combination 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.