-
Notifications
You must be signed in to change notification settings - Fork 0
/
416.partition-equal-subset-sum.py
53 lines (40 loc) · 1.48 KB
/
416.partition-equal-subset-sum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#
# @lc app=leetcode id=416 lang=python3
#
# [416] Partition Equal Subset Sum
#
# @lc code=start
class Solution:
def canPartition(self, nums: List[int]) -> bool: # 1-D memo
s = sum(nums)
n = len(nums)
if s % 2 != 0:
return False
target = s // 2
if max(nums) > target:
return False
memo = [False] * (target + 1)
memo[nums[0]] = True
for i in range(1, n):
for j in range(target, -1, -1): # we have to reverse traversal order. for example: [2,2,3,5], if don't reverse, because memo[2] == True, memo[4] = True; then because memo[4] == True, memo[6] = True
if memo[j] and (j + nums[i] <= target):
memo[j + nums[i]] = True
return memo[target]
def canPartition(self, nums: List[int]) -> bool: # 2-D memo
s = sum(nums)
n = len(nums)
if s % 2 != 0:
return False
target = s // 2
if max(nums) > target:
return False
memo = [[False] * (target + 1) for _ in range(n)] # memo[i] indicates the numbers that nums[0:i + 1] can sum to
memo[0][nums[0]] = True
for i in range(1, n):
for j in range(target + 1):
if memo[i - 1][j]:
memo[i][j] = True
if j + nums[i] <= target:
memo[i][j + nums[i]] = True
return memo[n - 1][target]
# @lc code=end