You are given an integer array matchsticks where matchsticks[i] is the length of the ith 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 <= 151 <= matchsticks[i] <= 108When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to determining if you can form a square with matchsticks involves trying every possible combination of assigning matchsticks to the four sides. We explore all combinations until we either find a valid square or exhaust all possibilities. Think of it like trying every possible arrangement of the sticks until you find one that works.
Here's how the algorithm would work step-by-step:
def matchsticks_to_square(matchsticks):
total_matchstick_length = sum(matchsticks)
# Impossible if total length isn't divisible by 4
if total_matchstick_length % 4 != 0:
return False
target_side_length = total_matchstick_length // 4
sides = [0] * 4
def can_form_square(index):
if index == len(matchsticks):
# Check if all sides are equal to the target
return all(side == target_side_length for side in sides)
for i in range(4):
# Skip if adding exceeds side length
if sides[i] + matchsticks[index] > target_side_length:
continue
sides[i] += matchsticks[index]
if can_form_square(index + 1):
return True
# Backtrack: remove to explore other paths
sides[i] -= matchsticks[index]
return False
return can_form_square(0)The core idea is to determine if the matchsticks can be divided into four equal groups, each forming a side of the square. Then, try to construct each side using the matchsticks in a way that efficiently explores possible combinations. This avoids testing every possible combination of matchsticks.
Here's how the algorithm would work step-by-step:
def matchsticks_to_square(matchsticks): total_matchstick_length = sum(matchsticks)
if total_matchstick_length % 4 != 0:
return False
side_length = total_matchstick_length // 4
matchsticks.sort(reverse=True)
number_of_matchsticks = len(matchsticks)
sides = [0] * 4
def can_form_square(index):
if index == number_of_matchsticks:
# If all sides are equal, we have a square
return sides[0] == side_length and sides[1] == side_length and sides[2] == side_length and sides[3] == side_length
for side_index in range(4):
# Try adding the current matchstick to each side
if sides[side_index] + matchsticks[index] <= side_length:
sides[side_index] += matchsticks[index]
if can_form_square(index + 1):
return True
# Backtrack if adding to current side doesn't work
sides[side_index] -= matchsticks[index]
return False
# Start recursive process, picking sticks
return can_form_square(0)| Case | How to Handle |
|---|---|
| Empty matchsticks array | Return false immediately, as a square cannot be formed with no matchsticks. |
| Matchsticks array with fewer than 4 elements | Return false, as at least 4 sides are needed to form a square. |
| Matchsticks array with one very long matchstick (longer than half the total length) | Return false, as this stick cannot possibly be used in a valid square. |
| Matchsticks array with many zeros | Zeros should not affect the algorithm's correctness but could cause unnecessary recursion; handle by skipping zero-length sticks. |
| Sum of matchsticks is not divisible by 4 | Return false immediately, as equal sides are impossible if the total length is not divisible by 4. |
| Integer overflow when calculating the sum of matchsticks | Use a long integer type to store the sum to prevent potential overflow. |
| The given array leads to excessive recursion depth (stack overflow) | Consider optimization techniques such as memoization or dynamic programming to reduce recursion. |
| All matchstick lengths are the same. | Check if the array length is a multiple of 4; if so, a square can be formed, otherwise not. |