Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c8e8e2e

Browse filesBrowse files
committed
added 15 June 2021
1 parent ee055ea commit c8e8e2e
Copy full SHA for c8e8e2e

File tree

Expand file treeCollapse file tree

1 file changed

+73
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+73
-0
lines changed
+73Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
class Solution(object):
2+
3+
def makesquare(self, nums):
4+
"""
5+
:type nums: List[int]
6+
:rtype: bool
7+
"""
8+
9+
# If there are no matchsticks, then we can't form any square.
10+
if not nums:
11+
return False
12+
13+
# Number of matchsticks
14+
L = len(nums)
15+
16+
# Possible perimeter of our square
17+
perimeter = sum(nums)
18+
19+
# Possible side of our square from the given matchsticks
20+
possible_side = perimeter // 4
21+
22+
# If the perimeter isn't equally divisible among 4 sides, return False.
23+
if possible_side * 4 != perimeter:
24+
return False
25+
26+
# Memoization cache for the dynamic programming solution.
27+
memo = {}
28+
29+
# mask and the sides_done define the state of our recursion.
30+
def recurse(mask, sides_done):
31+
32+
# This will calculate the total sum of matchsticks used till now using the bits of the mask.
33+
total = 0
34+
for i in range(L - 1, -1, -1):
35+
if not (mask & (1 << i)):
36+
total += nums[L - 1 - i]
37+
38+
# If some of the matchsticks have been used and the sum is divisible by our square's side, then we increment the number of sides completed.
39+
if total > 0 and total % possible_side == 0:
40+
sides_done += 1
41+
42+
# If we were successfully able to form 3 sides, return True
43+
if sides_done == 3:
44+
return True
45+
46+
# If this recursion state has already been calculated, just return the stored value.
47+
if (mask, sides_done) in memo:
48+
return memo[(mask, sides_done)]
49+
50+
# Common variable to store answer from all possible further recursions from this step.
51+
ans = False
52+
53+
# rem stores available space in the current side (incomplete).
54+
c = int(total / possible_side)
55+
rem = possible_side * (c + 1) - total
56+
57+
# Iterate over all the matchsticks
58+
for i in range(L - 1, -1, -1):
59+
60+
# If the current one can fit in the remaining space of the side and it hasn't already been taken, then try it out
61+
if nums[L - 1 - i] <= rem and mask & (1 << i):
62+
63+
# If the recursion after considering this matchstick gives a True result, just break. No need to look any further.
64+
# mask ^ (1 << i) makes the i^th from the right, 0 making it unavailable in future recursions.
65+
if recurse(mask ^ (1 << i), sides_done):
66+
ans = True
67+
break
68+
# cache the result for the current recursion state.
69+
memo[(mask, sides_done)] = ans
70+
return ans
71+
72+
# recurse with the initial mask with all matchsticks available.
73+
return recurse((1 << L) - 1, 0)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.