-
Notifications
You must be signed in to change notification settings - Fork 0
/
416.partition-equal-subset-sum.py
65 lines (49 loc) · 1.44 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
53
54
55
56
57
58
59
60
61
62
63
64
65
# Level: Medium
# TAGS: Array, Dynamic Programming
from typing import List
class Solution:
"""
DP Bottom-Up | Hash map
Time: O(N*T) | Space: O(T) with N is len(nums), T is target
Ref: https://leetcode.com/problems/partition-equal-subset-sum/solutions/462699/whiteboard-editorial-all-approaches-explained/
"""
def canPartition(self, nums: List[int]) -> bool:
total = sum(nums)
if total % 2:
return False
target = total // 2
visited = set([0])
for _, num in enumerate(nums):
visited |= {j + num for j in visited}
return target in visited
"""
DP Bottom-Up | Backtracking
Time: O(N*T) | Space: O(T) with N is len(nums), T is target
Choose current number or next number
"""
def canPartition1(self, nums: List[int]) -> bool:
total = sum(nums)
if total % 2:
return False
target = total // 2
memo = {}
def dfs(i, sofar):
if i == len(nums) or sofar > target:
return False
if sofar == target:
return True
if (i, sofar) in memo:
return memo[(i, sofar)]
memo[(i, sofar)] = dfs(i + 1, sofar + nums[i]) or dfs(i + 1, sofar)
return memo[(i, sofar)]
return dfs(0, 0)
tests = [
(
([1, 5, 11, 5],),
True,
),
(
([1, 2, 3, 5],),
False,
),
]