Implement def canpartitionequal(nums: list[int]) -> bool: subset sum partition.
- User
Implement
def can_partition_equal(nums: list[int]) -> bool:subset sum partition. Assistant
Implement def can_partition_equal(nums: list[int]) -> bool: subset sum partition.
Problem: Implement def can_partition_equal(nums: list[int]) -> bool: subset sum partition.
Approach: - dp[j] = Can we achieve sum j using some subset of processed elements?
dp[0] = True (empty subset achieves sum 0)num, update achievable sums from rKey considerations:
Observation 1: Necessary Condition
Falsedp[0] = True (empty subset achieves sum 0)num, update achievable sums from right to leftWhy iterate
Args: nums: List of non-negative integers
Returns: True if equal partition is possible, False otherwise
Time
Complexity: O(n sum(nums))
Let me implement this solution.
Given an array of integers, determine if it can be partitioned into two subsets with equal sums.
Key Insight: Finding two equal-sum subsets means each subset must sum to total_sum / 2. This reduces to the classic subset sum problem.
Falsetotal_sum / 2dp[j] = Can we achieve sum j using some subset of processed elements?dp[0] = True (empty subset achieves sum 0)num, update achievable sums from right to leftIterating right-to-left ensures each number is used at most once. Left-to-right would allow reuse.
def can_partition_equal(nums: list[int]) -> bool:
"""
Determines if the array can be partitioned into two subsets with equal sums.
Args:
nums: List of non-negative integers
Returns:
True if equal partition is possible, False otherwise
Time Complexity: O(n * sum(nums))
Space Complexity: O(sum(nums))
"""
# Calculate total sum
total_sum = sum(nums)
# If sum is odd, equal partition impossible
if total_sum % 2 != 0:
return False
# Target sum for each subset
target = total_sum // 2
# Handle edge case: if any single element equals target
if max(nums) > target:
return False
# dp[j] = True if sum j is achievable
dp = [False] * (target + 1)
dp[0] = True # Empty subset achieves sum 0
# Process each number
for num in nums:
# Iterate from target down to num (right to left)
# This ensures each number is used at most once
for j in range(target, num - 1, -1):
dp[j] = dp[j] or dp[j - num]
# Early termination if target is achieved
if dp[target]:
return True
return dp[target]
[1, 5, 11, 5]Total sum = 22, Target = 11
Initial: dp = [T, F, F, F, F, F, F, F, F, F, F, F]
After processing 1:
dp[1] = dp[1] OR dp[0] = True
dp = [T, T, F, F, F, F, F, F, F, F, F, F]
After processing 5:
dp[6] = dp[6] OR dp[1] = True
dp[5] = dp[5] OR dp[0] = True
dp = [T, T, F, F, F, T, T, F, F, F, F, F]
After processing 11:
dp[11] = dp[11] OR dp[0] = True ✓
Result: True (Partition: [11] and [1,5,5])
[1, 2, 3, 5]Total sum = 11 (odd)
Result: False (immediately)
[1, 2, 3, 4]Total sum = 10, Target = 5
After processing 1: dp[1] = True
After processing 2: dp[2]=True, dp[3]=True
After processing 3: dp[5]=True ✓
Result: True (Partition: [1,4] and [2,3])
| Metric | Complexity |
|---|---|
| Time | O(n × target) where target = sum/2 |
| Space | O(target) for the 1D DP array |
def can_partition_equal_set(nums: list[int]) -> bool:
"""Using set for potentially better average case performance."""
total_sum = sum(nums)
if total_sum % 2 != 0:
return False
target = total_sum // 2
achievable = {0}
for num in nums:
new_sums = {s + num for s in achievable if s + num <= target}
achievable.update(new_sums)
if target in achievable:
return True
return target in achievable
This set-based approach can be faster when the number of achievable sums is sparse.