We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 81c294e commit 9846735Copy full SHA for 9846735
473-Matchsticks-to-Square.py
@@ -0,0 +1,21 @@
1
+class Solution:
2
+ def makesquare(self, matchsticks: List[int]) -> bool:
3
+ length = sum(matchsticks) // 4
4
+ sides = [0] * 4
5
+
6
+ if sum(matchsticks) / 4 != length:
7
+ return False
8
+ matchsticks.sort(reverse=True)
9
+ def backtrack(i):
10
+ if i == len(matchsticks):
11
+ return True
12
13
+ for j in range(4):
14
+ if sides[j] + matchsticks[i] <= length:
15
+ sides[j] += matchsticks[i]
16
+ if backtrack(i + 1):
17
18
+ sides[j] -= matchsticks[i]
19
20
21
+ return backtrack(0)
0 commit comments