Tensorum
YouTubeसीखना शुरू करें
LeetCode 416 · 0/1 Knapsack / Subset Sum

Partition Equal Subset Sum

LeetCode 416 · मध्यम · समाधान, intuition और कदम-दर-कदम वॉकथ्रू

Equal partition ⇔ a subset sums to total / 2 (and total must be even).

मध्यमArrayDynamic ProgrammingKnapsack Problem0-1 Knapsack

मैंने Partition Equal Subset Sum (LeetCode 416) ऐसे हल किया — 0/1 Knapsack / Subset Sum पैटर्न का एक मध्यम प्रॉब्लम। नीचे: intuition, कदम-दर-कदम approach, मेरा accepted Python कोड, animated test cases और line-by-line debugger।

नीचे का विवरण अंग्रेज़ी में है।

Intuition

Splitting into two equal halves means finding a subset that sums to exactly half the total. That is the classic 0/1 subset-sum question: for each number, either include it or not, and track which sums are reachable.

Approach

  1. 1

    If the total is odd, return False immediately.

  2. 2

    dp[i][t] = can the first i+1 numbers make sum t?

  3. 3

    Sum 0 is always reachable (empty subset); nums[0] is reachable from row 0.

  4. 4

    For each later number: exclude it (copy the row above) or include it (look up t − nums[i] in the row above).

  5. 5

    Answer is dp[n-1][target].

मैंने कैसे हल किया

Subset-sum tabulationPythonO(n·S) time · O(n·S) space
class Solution:
    def isSubsetSum (self, nums, target):
        # code here 
        # memo={}
        # def dfs(index,curTarget):
           
        #     if curTarget==0:
        #         return True
        #     if curTarget<0 or index<0:
        #         return False
        #     key=(index,curTarget)
        #     if key in memo:
        #         return memo[key]
            
        #     exclude = dfs(index-1,curTarget)
        #     include = False
        #     if nums[index]<=curTarget:
        #         include = dfs(index-1,curTarget-nums[index])
        #     memo[key]= include or exclude
        #     return memo[key]
        # return dfs(len(nums)-1,target)
        n =len(nums)
        dp=[[False]*(target+1) for _ in range(n)]
        # Every number can make sum 0 with an empty subset
        for i in range(n):
            dp[i][0] = True
            
        if nums[0]<=target: #it must not cross the target
            dp[0][nums[0]]=True
    
        for index in range(1, n):
            for curTarget in range(1, target + 1):
                exclude = dp[index - 1][curTarget]
                include = False
                if nums[index] <= curTarget:
                    include = dp[index - 1][curTarget - nums[index]]
                dp[index][curTarget] = include or exclude
    
        return dp[n - 1][target]
    def canPartition(self, nums: List[int]) -> bool:
        totalsum = sum(nums)
        if totalsum%2!=0:
            return False
        target = totalsum//2
        return self.isSubsetSum(nums,target)
स्टेप-थ्रू लोड हो रहा है…

Partition Equal Subset Sum — अक्सर पूछे जाने वाले प्रश्न

Partition Equal Subset Sum के पीछे intuition क्या है?

Splitting into two equal halves means finding a subset that sums to exactly half the total. That is the classic 0/1 subset-sum question: for each number, either include it or not, and track which sums are reachable.

Partition Equal Subset Sum (LeetCode 416) कैसे हल करें?

1. If the total is odd, return False immediately. 2. dp[i][t] = can the first i+1 numbers make sum t? 3. Sum 0 is always reachable (empty subset); nums[0] is reachable from row 0. 4. For each later number: exclude it (copy the row above) or include it (look up t − nums[i] in the row above). 5. Answer is dp[n-1][target].

Partition Equal Subset Sum की time और space complexity क्या है?

Time: O(n · target)। Space: O(n · target)। (Accepted approach: Subset-sum tabulation।)

Partition Equal Subset Sum में आम गलतियाँ क्या हैं?

Only seed dp[0][nums[0]] when nums[0] ≤ target. A 1-D boolean array iterated right-to-left would cut space to O(target).