Partition Equal Subset Sum
LeetCode 416 · మధ్యస్థం · పరిష్కారం, intuition & దశలవారీ వాక్త్రూ
Equal partition ⇔ a subset sums to total / 2 (and total must be even).
నేను 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
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].
నేను ఎలా పరిష్కరించాను
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).