class Solution:
def makesquare(self, matchsticks: List[int]) -> bool:
n=len(matchsticks)
if sum(matchsticks)%4!=0: return False
length=sum(matchsticks)//4
side=[0]*4
matchsticks.sort(reverse=True)
if matchsticks[0]>length:return False
def backtrack(i):
if i==n:return True
for j in range(4):
if side[j]+matchsticks[i]<=length:
side[j]+=matchsticks[i]
if backtrack(i+1):return True
side[j]-=matchsticks[i]
if side[j]==0:break
return False
return backtrack(0)
这个题有两处剪枝
1)任意stick不能长过边长
2)任意边长度大于0