Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match girl has, please find out a way you can make one square by using up all those matchsticks. You should not break any stick, but you can link them up, and each matchstick must be used exactly one time.
Your input will be several matchsticks the girl has, represented with their stick length. Your output will either be true or false, to represent whether you could make one square using all the matchsticks the little match girl has.
Example 1:
Input: [1,1,2,2,2] Output: trueExplanation: You can form a square with length 2, one side of the square came two sticks with length 1.
Example 2:
Input: [3,3,3,3,4] Output: falseExplanation: You cannot find a way to form a square with all the matchsticks.
Note:
- The length sum of the given matchsticks is in the range of
0to10^9. - The length of the given matchstick array will not exceed
15.
Related Topics:
Depth-first Search
// OJ: https://leetcode.com/problems/matchsticks-to-square/
// Author: github.com/lzl124631x
// Time: O(N * 2^N)
// Space: O(2^N)
class Solution {
vector<int> dp;
bool dfs(vector<int> &A, int used, int todo, int target) {
if (dp[used] != -1) return dp[used];
dp[used] = 0;
int mx = todo % target;
if (mx == 0) mx = target;
for (int i = 0; i < A.size() && !dp[used]; ++i) {
dp[used] = ((used >> i) & 1) == 0 && A[i] <= mx && dfs(A, used | (1 << i), todo - A[i], target);
}
return dp[used];
}
public:
bool makesquare(vector<int>& A) {
if (A.empty()) return false;
int len = accumulate(begin(A), end(A), 0);
if (len % 4 || *max_element(begin(A), end(A)) > len / 4) return false;
dp.assign(1 << A.size(), -1);
dp[(1 << A.size()) - 1] = 1;
return dfs(A, 0, len, len / 4);
}
};Let target = sum(A) / 4, which is the target length of each edge.
We use DFS to try to fill each A[i] into different edges.
Two optimizations here:
- The
unordered_set<int> seenis used to prevent handling the same edge value again. For example, assumeedge = [1, 1, 1, 1], and now we are trying to add a stick of length2to it. Adding2to either1will yield the same result. So we just need to add to a edge with length1once. - Sorting the sticks in descending order will make it converge faster because it's easy to fill in sands but hard to fill in peddles; filling peddles first will fail faster.
// OJ: https://leetcode.com/problems/matchsticks-to-square/
// Author: github.com/lzl124631x
// Time: O(4^N)
// Space: O(N * SUM(A))
class Solution {
int edge[4] = {}, target;
bool dfs(vector<int> &A, int i) {
if (i == A.size()) return true;
unordered_set<int> seen;
for (int j = 0; j < 4; ++j) {
if (seen.count(edge[j]) || edge[j] + A[i] > target) continue;
seen.insert(edge[j]);
edge[j] += A[i];
if (dfs(A, i + 1)) return true;
edge[j] -= A[i];
}
return false;
}
public:
bool makesquare(vector<int>& A) {
if (A.size() < 4) return false;
int sum = accumulate(begin(A), end(A), 0);
if (sum % 4 || *max_element(begin(A), end(A)) > sum / 4) return false;
target = sum / 4;
sort(begin(A), end(A), greater<>());
return dfs(A, 0);
}
};