MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / canPartition

Method canPartition

python/0416-partition-equal-subset-sum.py:2–18  ·  view source on GitHub ↗
(self, nums: List[int])

Source from the content-addressed store, hash-verified

1class Solution:
2 def canPartition(self, nums: List[int]) -> bool:
3 if sum(nums) % 2:
4 return False
5
6 dp = set()
7 dp.add(0)
8 target = sum(nums) // 2
9
10 for i in range(len(nums) - 1, -1, -1):
11 nextDP = set()
12 for t in dp:
13 if (t + nums[i]) == target:
14 return True
15 nextDP.add(t + nums[i])
16 nextDP.add(t)
17 dp = nextDP
18 return False

Callers

nothing calls this directly

Calls 2

sumFunction · 0.50
addMethod · 0.45

Tested by

no test coverage detected