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