-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathpartition-equal-subset-sum.py
73 lines (50 loc) · 1.85 KB
/
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
66
67
68
69
70
71
72
73
from typing import List
from functools import lru_cache
class Solution:
def canPartitionTopDown(self, nums: List[int]) -> bool:
total = sum(nums)
@lru_cache(None)
def knapsack(capacity: int, pos: int) -> bool:
if pos >= len(nums):
return False
if capacity > total / 2:
return False
if capacity == total / 2:
return True
pick_this = knapsack(capacity + nums[pos], pos + 1)
skip_this = knapsack(capacity, pos + 1)
return pick_this or skip_this
return knapsack(0, 0)
def canPartition(self, nums: List[int]) -> bool:
total = sum(nums)
half = total / 2
dp = []
for pos in range(len(nums)):
dp.append([False] * int(half + 1))
dp[pos][0] = True
for capacity in range(1, int(half + 1)):
if pos == 0:
dp[pos][capacity] = capacity == nums[pos]
continue
dp[pos][capacity] = dp[pos - 1][capacity]
if capacity >= nums[pos]:
dp[pos][capacity] = dp[pos][capacity] or dp[pos - 1][capacity - nums[pos]]
if capacity == half and dp[pos][capacity]:
return True
return False
def canPartition2(self, nums: List[int]) -> bool:
@lru_cache(None)
def dp(pos: int, left: int) -> bool:
if left == 0:
return True
if pos == len(nums):
return False
if dp(pos + 1, left):
return True
if nums[pos] <= left and dp(pos + 1, left - nums[pos]):
return True
return False
total = sum(nums)
if total % 2 != 0:
return False
return dp(0, total / 2)