Algorithm
Problem Name: 473. 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
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
class Solution {
public:
bool makesquare(vector<int>& nums) {
int sum = 0, maxlen = 0, n = nums.size();
for(int x: nums) sum += x, maxlen = max(maxlen, x);
int size = sum / 4;
if(sum == 0 || sum % 4 || maxlen > size ) return false;
vector<int>visited(n, 0);
return DFS(nums, visited, 0, 0, size, n, 0, false);
}
bool DFS(vector<int>& nums, vector<int>& visited, int pos, int length, int size, int n, int count, bool newSide){
if(length > size) return false;
if(length == size) count++, length = 0, newSide = true;
if(count == 3) return true;
length += nums[pos];
visited[pos] = 1;
for(int i = newSide ? 0 : pos + 1; i < n; i++)
if(!visited[i] && DFS(nums, visited, i, length, size, n, count, false)>
return true;
visited[pos] = 0;
return false;
}
};
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with Java Programming
Code -
Java Programming
class Solution {
public boolean makesquare(int[] matchsticks) {
int n = matchsticks.length;
int sum = 0;
for (int stick : matchsticks) {
sum += stick;
}
int possibleSide = sum / 4;
if (possibleSide * 4 != sum) {
return false;
}
Arrays.sort(matchsticks);
return dfs(new int[4], matchsticks, possibleSide, matchsticks.length - 1);
}
private boolean dfs(int[] sum, int[] sticks, int possibleSide, int idx) {
if (idx == -1) {
return sum[0] == sum[1] && sum[1] == sum[2] && sum[2] == sum[3];
}
int stick = sticks[idx];
for (int i = 0; i < 4; i++) {
if (sum[i] + stick <= possibleSide) {
sum[i] += stick;
if (dfs(sum, sticks, possibleSide, idx - 1)) {
return true;
}
sum[i] -= stick;
}
}
return false;
}
}
Copy The Code &
Try With Live Editor
Input
Output
#3 Code Example with Javascript Programming
Code -
Javascript Programming
const makesquare = function(nums) {
if (nums == null || nums.length < 4) return false
const sum = nums.reduce((ac, el) => ac + el, 0)
if (sum % 4 !== 0) return false
nums.sort((a, b) => b - a)
return dfs(nums, new Array(4).fill(0), 0, sum / 4)
}
function dfs(nums, arr, idx, target) {
if (idx === nums.length) {
return true
}
for (let i = 0; i < 4; i++) {
if (arr[i] + nums[idx] > target || (i > 0 && arr[i] === arr[i - 1]))
continue
arr[i] += nums[idx]
if (dfs(nums, arr, idx + 1, target)) return true
arr[i] -= nums[idx]
}
return false
}
Copy The Code &
Try With Live Editor
Input
Output
#4 Code Example with Python Programming
Code -
Python Programming
class Solution:
def makesquare(self, nums):
def dfs(index, edge, count, used):
for i in range(index, len(nums)):
if i in used or edge - nums[i] < 0: continue
elif edge - nums[i] > 0 and dfs(i + 1, edge - nums[i], count, used | {i}): return True
elif edge - nums[i] == 0 and (count and dfs(1, l, count - 1, used | {i})) or not count: return True
return False
sm = sum(nums)
if len(nums) < 4 or sm % 4 != 0: return False
l = sm // 4
nums.sort(reverse = True)
if nums[0] > l: return False
return nums[0] == l and dfs(1, l, 1, {0}) or dfs(1, l - nums[0], 2, {0})
Copy The Code &
Try With Live Editor
Input
Output