Skip to content

Commit 2b476fc

Browse files
committed
Update 416. Partition Equal Subset Sum.py
1 parent 6d632e3 commit 2b476fc

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def canPartition(self, nums: List[int]) -> bool:
3+
m, mod = divmod(sum(nums), 2)
4+
if mod:
5+
return False
6+
n = len(nums)
7+
f = [[False] * (m + 1) for _ in range(n + 1)]
8+
f[0][0] = True
9+
for i, x in enumerate(nums, 1):
10+
for j in range(m + 1):
11+
f[i][j] = f[i - 1][j] or (j >= x and f[i - 1][j - x])
12+
return f[n][m]

0 commit comments

Comments
 (0)