-
Notifications
You must be signed in to change notification settings - Fork 43
/
partition-equal-subset-sum.py
170 lines (142 loc) · 4.72 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
"""
416. Partition Equal Subset Sum
Medium
Given a non-empty array nums containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.
Example 1:
Input: nums = [1,5,11,5]
Output: true
Explanation: The array can be partitioned as [1, 5, 5] and [11].
Example 2:
Input: nums = [1,2,3,5]
Output: false
Explanation: The array cannot be partitioned into equal sum subsets.
Constraints:
1 <= nums.length <= 200
1 <= nums[i] <= 100
"""
# VO
# V1
# IDEA : brute force (TLE)
# https://leetcode.com/problems/partition-equal-subset-sum/solution/
class Solution:
def canPartition(self, nums: List[int]) -> bool:
def dfs(nums: List[int], n: int, subset_sum: int) -> bool:
# Base cases
if subset_sum == 0:
return True
if n == 0 or subset_sum < 0:
return False
result = (dfs(nums, n - 1, subset_sum - nums[n - 1])
or dfs(nums, n - 1, subset_sum))
return result
# find sum of array elements
total_sum = sum(nums)
# if total_sum is odd, it cannot be partitioned into equal sum subsets
if total_sum % 2 != 0:
return False
subset_sum = total_sum // 2
n = len(nums)
return dfs(nums, n - 1, subset_sum)
# V1'
# IDEA : Top Down Dynamic Programming - Memoization
# https://leetcode.com/problems/partition-equal-subset-sum/solution/
class Solution:
def canPartition(self, nums: List[int]) -> bool:
@lru_cache(maxsize=None)
def dfs(nums: Tuple[int], n: int, subset_sum: int) -> bool:
# Base cases
if subset_sum == 0:
return True
if n == 0 or subset_sum < 0:
return False
result = (dfs(nums, n - 1, subset_sum - nums[n - 1])
or dfs(nums, n - 1, subset_sum))
return result
# find sum of array elements
total_sum = sum(nums)
# if total_sum is odd, it cannot be partitioned into equal sum subsets
if total_sum % 2 != 0:
return False
subset_sum = total_sum // 2
n = len(nums)
return dfs(tuple(nums), n - 1, subset_sum)
# V1''
# IDEA : Bottom Up Dynamic Programming
# https://leetcode.com/problems/partition-equal-subset-sum/solution/
class Solution:
def canPartition(self, nums: List[int]) -> bool:
# find sum of array elements
total_sum = sum(nums)
# if total_sum is odd, it cannot be partitioned into equal sum subsets
if total_sum % 2 != 0:
return False
subset_sum = total_sum // 2
n = len(nums)
# construct a dp table of size (n+1) x (subset_sum + 1)
dp = [[False] * (subset_sum + 1) for _ in range(n + 1)]
dp[0][0] = True
for i in range(1, n + 1):
curr = nums[i - 1]
for j in range(subset_sum + 1):
if j < curr:
dp[i][j] = dp[i - 1][j]
else:
dp[i][j] = dp[i - 1][j] or dp[i - 1][j - curr]
return dp[n][subset_sum]
# V1'''
# https://blog.csdn.net/fuxuemingzhu/article/details/79787425
# DFS
class Solution:
def canPartition(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
_sum = sum(nums)
div, mod = divmod(_sum, 2)
if mod or max(nums) > div: return False
nums.sort(reverse = True)
target = [div] * 2
return self.dfs(nums, 0, target)
def dfs(self, nums, index, target):
for i in range(2):
if target[i] >= nums[index]:
target[i] -= nums[index]
if target[i] == 0 or self.dfs(nums, index + 1, target): return True
target[i] += nums[index]
return False
# V1'''''
# https://blog.csdn.net/fuxuemingzhu/article/details/79787425
# dp[j] = dp[j] || dp[j - nums[i]]
class Solution(object):
def canPartition(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
sums = sum(nums)
if sums & 1: return False
nset = set([0])
for n in nums:
for m in nset.copy():
nset.add(m + n)
return sums / 2 in nset
# V2
# Time: O(n * s), s is the sum of nums
# Space: O(s)
class Solution(object):
def canPartition(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
s = sum(nums)
if s % 2:
return False
dp = [False] * (s/2 + 1)
dp[0] = True
for num in nums:
for i in reversed(xrange(1, len(dp))):
if num <= i:
dp[i] = dp[i] or dp[i - num]
return dp[-1]