Partition Equal Subset Sum
LeetCode 416 · Medium · solution, intuition & step-by-step walkthrough
Equal partition ⇔ a subset sums to total / 2 (and total must be even).
This is how I solved Partition Equal Subset Sum (LeetCode 416), a medium problem from the 0/1 Knapsack / Subset Sum pattern. Below: the intuition, the approach step by step, my accepted Python code, animated test cases and a 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].
How I solved it
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 — FAQ
What is the intuition behind Partition Equal Subset Sum?
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.
How do you solve 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].
What is the time and space complexity of Partition Equal Subset Sum?
Time: O(n · target). Space: O(n · target). (Accepted approach: Subset-sum tabulation.)
What are common mistakes in 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).