-
Notifications
You must be signed in to change notification settings - Fork 1
/
473.py
77 lines (65 loc) · 2.33 KB
/
473.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
# 注意,维护nums是没有意义的,因为有可能某些拿数的情况下是不行的
class Solution:
def makesquare(self, matchsticks: List[int]) -> bool:
target = sum(matchsticks)
if target % 4 != 0:
return False
else:
target //= 4
n = len(matchsticks)
@cache
def dfs(status, tarnow):
if status == (1 << n) - 1 and tarnow == target:
return True
for i in range(n):
if (1 << i) & status == 0:
if matchsticks[i] > tarnow:
continue
if matchsticks[i] == tarnow:
return dfs(status | (1 << i), target)
else:
if dfs(status | (1 << i), tarnow - matchsticks[i]):
return True
return False
return dfs(0, target)
# 根据题解可以将小于等于合并
class Solution:
def makesquare(self, matchsticks: List[int]) -> bool:
target = sum(matchsticks)
if target % 4 != 0:
return False
else:
target //= 4
n = len(matchsticks)
nums = 0
@cache
def dfs(status, tarnow):
nonlocal nums
if tarnow == 0:
tarnow = target
if status == (1 << n) - 1:
return True
for i in range(n):
if (1 << i) & status == 0 and matchsticks[i] <= tarnow:
if dfs(status | (1 << i), tarnow - matchsticks[i]):
return True
return False
return dfs(0, target)
# 答案不是记忆化搜索,时间上差了很多
class Solution:
def makesquare(self, matchsticks: List[int]) -> bool:
totalLen = sum(matchsticks)
if totalLen % 4:
return False
tLen = totalLen // 4
dp = [-1] * (1 << len(matchsticks))
dp[0] = 0
for s in range(1, len(dp)):
for k, v in enumerate(matchsticks):
if s & (1 << k) == 0:
continue
s1 = s & ~(1 << k)
if dp[s1] >= 0 and dp[s1] + v <= tLen:
dp[s] = (dp[s1] + v) % tLen
break
return dp[-1] == 0