#416
Partition Equal Subset Sum
medium· 1-D DPruns: 0Given an integer array nums, return true if you can partition the array into two subsets such that the sum of the elements in both subsets is equal, otherwise return false.
sign in to paste and practice your own solution
wpm 0acc 100%time 0:000 / 308
class Solution:
def canPartition(self, nums: list[int]) -> bool:
total = sum(nums)
if total % 2:
return False
target = total // 2
reachable = {0}
for num in nums:
reachable |= {s + num for s in reachable}
return target in reachable
click the box to focus · tab inserts 4 spaces · backspace to correct · esc to pause
desktop only
codedrill is a typing game and needs a real keyboard. open this on a laptop or desktop to practice.
you can still browse problems and sections from your phone.