forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matchsticks-to-square.py
36 lines (29 loc) · 1.12 KB
/
matchsticks-to-square.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
# Time: O(n * s * 2^n), s is the number of subset of which sum equals to side length.
# Space: O(n * (2^n + s))
class Solution(object):
def makesquare(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
total_len = sum(nums)
if total_len % 4:
return False
side_len = total_len / 4
fullset = (1 << len(nums)) - 1
used_subsets = []
valid_half_subsets = [0] * (1 << len(nums))
for subset in xrange(fullset+1):
subset_total_len = 0
for i in xrange(len(nums)):
if subset & (1 << i):
subset_total_len += nums[i]
if subset_total_len == side_len:
for used_subset in used_subsets:
if (used_subset & subset) == 0:
valid_half_subset = used_subset | subset
valid_half_subsets[valid_half_subset] = True
if valid_half_subsets[fullset ^ valid_half_subset]:
return True
used_subsets.append(subset)
return False