Matchsticks To Square Problem
Description
LeetCode Problem 473.
You are given an integer array matchsticks where matchsticks[i] is the length of the i^th matchstick. You want to use all the matchsticks to make one square. You should not break any stick, but you can link them up, and each matchstick must be used exactly one time.
Return true if you can make this square and false otherwise.
Example 1:
Input: matchsticks = [1,1,2,2,2]
Output: true
Explanation: You can form a square with length 2, one side of the square came two sticks with length 1.
Example 2:
Input: matchsticks = [3,3,3,3,4]
Output: false
Explanation: You cannot find a way to form a square with all the matchsticks.
Constraints:
- 1 <= matchsticks.length <= 15
- 1 <= matchsticks[i] <= 10^8
Solution
Approach: Bitmask DP over the Used Sticks
The four sides must have equal length, so if the total is not divisible by 4 the answer is false. Otherwise every side has length side = total / 4.
Build the sides one at a time: keep adding sticks to the current side, and when it reaches exactly side, start the next one. With at most 15 sticks, the set of sticks used so far fits in a bitmask (215 = 32768 sets).
The key observation: once we know which sticks are used, we also know how much of the current side is filled. It is (sum of the used sticks) % side, whatever order they were placed in. So one number per mask is enough:
fill[mask]is that partial length, or-1if the sticks inmaskcannot be placed without overshooting a side.- Start with
fill[0] = 0. - For each reachable
maskand each unused sticki: iffill[mask] + matchsticks[i] <= side, the stick fits, andfill[mask | (1 << i)] = (fill[mask] + matchsticks[i]) % side. The% sideturns a just-finished side into 0, the start of the next one.
The answer is true when the mask with every stick is reachable with fill equal to 0: the sticks then form exactly four full sides. Masks only grow, so visiting them in increasing order handles each one after everything that leads to it.
Trying every assignment of sticks to four sides without remembering anything can take up to 415 steps; the DP visits each mask once.
Complexity: O(n · 2n) time and O(2n) space, where n is the number of matchsticks.
class Solution:
def makesquare(self, matchsticks: List[int]) -> bool:
total = sum(matchsticks)
if total % 4 != 0:
return False
side = total // 4
count = len(matchsticks)
full = (1 << count) - 1
# fill[mask] = length already laid on the side being built, after using the sticks in mask
# (sides are filled one at a time); -1 means mask cannot be reached.
fill = [-1] * (1 << count)
fill[0] = 0
for mask in range(1 << count):
if fill[mask] == -1:
continue
for i in range(count):
if mask & (1 << i):
continue # stick i is already used
length = fill[mask] + matchsticks[i]
if length <= side:
# A side that reaches exactly `side` is finished, and the next one starts at 0.
fill[mask | (1 << i)] = length % side
return fill[full] == 0
Now write it from memory
Close the write-up and solve it yourself against the tests.