Skip to content

Commit 72377f2

Browse files
committed
Runtime: 437 ms (Top 80.03%) | Memory: 18.00 MB (Top 70.21%)
1 parent a8243a3 commit 72377f2

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1+
// Runtime: 437 ms (Top 80.03%) | Memory: 18.00 MB (Top 70.21%)
2+
13
class Solution:
24
def canPartition(self, nums: List[int]) -> bool:
3-
total = sum(nums)
4-
if total % 2: return False
5-
total //= 2
6-
leng = len(nums)
7-
dp = [[False] * (total + 1) for _ in range(leng + 1)]
8-
9-
for i in range(leng + 1): dp[i][0] = True
10-
for i in range(1, leng + 1):
11-
for j in range(1, total + 1):
12-
dp[i][j] = dp[i-1][j]
13-
if j - nums[i-1] >= 0:
14-
dp[i][j] |= dp[i-1][j-nums[i-1]]
15-
return dp[leng][total]
5+
dp, s = set([0]), sum(nums)
6+
if s&1:
7+
return False
8+
for num in nums:
9+
for curr in range(s>>1, num-1, -1):
10+
if curr not in dp and curr-num in dp:
11+
if curr == s>>1:
12+
return True
13+
dp.add(curr)
14+
return False

0 commit comments

Comments
 (0)