- Based on the size of constraints, it’s solvable through
- Do we need to track
Nobecause after having form a side, we need to keep find more and have to preserve curr sides. So every time we need to iterate from
0. (Just image when recursion is pretty deep and
startIndexwould be really big. If we do have
startIndex, then we have no access to very front elements.)
- Edge case to trim unnecessary dfs: Side target is 18, and an element is 16, but every element is longer than 2.
- Use a
indexthat we’ve used.
- If we don’t sort it first, then we might have really deep recursions if the front elements are really small. So we’d better put big element in the front.
You are given an integer array
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.
true if you can make this square and
Input: matchsticks = [1,1,2,2,2]