Matchsticks to Square
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 <= 15
1 <= matchsticks[i] <= 108
Solution
We are looking to build four sides of the same length, and we can easily calculate the target side length by sum(matchsticks)//4
.
Then we can solve this problem by trying to put each matchstick on the four different sides, and see if we can reach a solution.
We can apply the backtracking2 template for aggregation, as we are only asked to find whether a solution exists.
Let's fill in the logic.
is_leaf
:start_index == len(matchsticks)
, when we have used all of the matchsticks.get_edges
: we can put a match stick on four different sides.is_valid
:sides[i] + matchsticks[start_index] <= side_length
, the choice is valid only when the newside[i]
length will not exceedside_length
.- additinal states:
sides
that stores the current length of the four sides.
Implementation
def makesquare(self, matchsticks: List[int]) -> bool:
if sum(matchsticks) % 4 != 0: return False
side_length = sum(matchsticks) // 4
matchsticks.sort(reverse=True)
sides = [0, 0, 0, 0]
def dfs(start_index):
if start_index == len(matchsticks):
return side_length == sides[0] == sides[1] == sides[2] == sides[3]
for i in range(4):
if sides[i] + matchsticks[start_index] <= side_length:
sides[i] += matchsticks[start_index]
if dfs(start_index + 1): return True
sides[i] -= matchsticks[start_index]
return False
return dfs(0)
Ready to land your dream job?
Unlock your dream job with a 2-minute evaluator for a personalized learning plan!
Start EvaluatorWhat are the two properties the problem needs to have for dynamic programming to be applicable? (Select 2)
Recommended Readings
LeetCode Patterns Your Personal Dijkstra's Algorithm to Landing Your Dream Job The goal of AlgoMonster is to help you get a job in the shortest amount of time possible in a data driven way We compiled datasets of tech interview problems and broke them down by patterns This way we
Recursion Recursion is one of the most important concepts in computer science Simply speaking recursion is the process of a function calling itself Using a real life analogy imagine a scenario where you invite your friends to lunch https algomonster s3 us east 2 amazonaws com recursion jpg You first
Runtime Overview When learning about algorithms and data structures you'll frequently encounter the term time complexity This concept is fundamental in computer science and offers insights into how long an algorithm takes to complete given a certain input size What is Time Complexity Time complexity represents the amount of time
Want a Structured Path to Master System Design Too? Don’t Miss This!