当前位置:网站首页>【Leetcode】473. Matchsticks to Square
【Leetcode】473. Matchsticks to Square
2022-08-01 23:47:00 【记录算法题解】
题目地址:
https://leetcode.com/problems/matchsticks-to-square/
给定一个长 n n n正整数数组 A A A,代表若干个小木棍的长度,要求将这些小木棍拼成一个大正方形,使得所有的小木棍都恰好用完。问是否可能。
参考https://blog.csdn.net/qq_46105170/article/details/115562806。我们可以从长到短枚举当前应该放哪个小木棍,剪枝可以参考链接。代码如下:
class Solution {
public:
int len;
bool makesquare(vector<int> &A) {
len = 0;
for (auto &x : A) len += x;
// 不是4的倍数,说明无解
if (len % 4) return false;
len /= 4;
sort(A.begin(), A.end(), greater<int>());
vector<bool> vis(A.size(), 0);
return dfs(0, 0, 0, A, vis);
}
bool dfs(int u, int cur_len, int cnt, vector<int> &A, vector<bool> &vis) {
if (cnt == 3) return true;
if (cur_len == len) return dfs(0, 0, cnt + 1, A, vis);
for (int i = u; i < A.size(); i++) {
if (vis[i]) continue;
if (cur_len + A[i] > len) continue;
cur_len += A[i];
vis[i] = true;
if (dfs(u + 1, cur_len, cnt, A, vis)) return true;
cur_len -= A[i];
vis[i] = false;
if (!cur_len || cur_len + A[i] == len) return false;
while (i + 1 < A.size() && A[i + 1] == A[i]) i++;
}
return false;
}
};
时间复杂度指数级,空间 O ( n ) O(n) O(n)。
Java:
import java.util.Arrays;
public class Solution {
public boolean makesquare(int[] A) {
int len = 0;
for (int x : A) {
len += x;
}
if (len % 4 != 0) {
return false;
}
len /= 4;
Arrays.sort(A);
reverse(A);
boolean[] vis = new boolean[A.length];
return dfs(0, 0, len, 0, A, vis);
}
void reverse(int[] A) {
for (int i = 0, j = A.length - 1; i < j; i++, j--) {
int tmp = A[i];
A[i] = A[j];
A[j] = tmp;
}
}
boolean dfs(int u, int curLen, int len, int cnt, int[] A, boolean[] vis) {
if (cnt == 3) {
return true;
}
if (curLen == len) {
return dfs(0, 0, len, cnt + 1, A, vis);
}
for (int i = u; i < A.length; i++) {
if (vis[i] || curLen + A[i] > len) {
continue;
}
vis[i] = true;
curLen += A[i];
if (dfs(u + 1, curLen, len, cnt, A, vis)) {
return true;
}
vis[i] = false;
curLen -= A[i];
if (curLen == 0 || curLen + A[i] == len) {
return false;
}
while (i + 1 < A.length && A[i + 1] == A[i]) {
i++;
}
}
return false;
}
}
时空复杂度一样。
边栏推荐
- Docker实践经验:Docker 上部署 mysql8 主从复制
- [LeetCode304 Weekly Competition] Two questions about the base ring tree 6134. Find the closest node to the given two nodes, 6135. The longest cycle in the graph
- 6134. Find the closest node to the given two nodes - force double hundred code
- 【MySQL系列】MySQL索引事务
- 带你搞懂MySQL隔离级别,两个事务同时操作同一行数据会怎样?
- cdh6打开oozieWeb页面,Oozie web console is disabled.
- 使用Ganache、web3.js和remix在私有链上部署并调用合约
- solidity
- 6133. 分组的最大数量
- 6132. 使数组中所有元素都等于零-快速排序法
猜你喜欢
[C language advanced] file operation (2)
分享一份接口测试项目(非常值得练手)
cmd command
chrome copies the base64 data of an image
The third chapter of the imitation cattle network project: develop the core functions of the community (detailed steps and ideas)
6134. Find the closest node to the given two nodes - force double hundred code
A brief analysis of mobile APP security testing in software testing, shared by a third-party software testing agency in Beijing
cdh的hue上oozie启动报错,Cannot allocate containers as requested resource is greater than maximum allowed
Thymeleaf简介
Solve the port to take up
随机推荐
PostgreSQL Basics--Common Commands
Calculate the midpoint between two points
Quartus uses tcl files to quickly configure pins
伸展树的特性及实现
Flink学习第三天——一文带你了解什么是Flink流?
How do programmers solve online problems gracefully?
Programmer is still short of objects? A new one is enough
Flink Yarn Per Job - Yarn应用
Bean的生命周期
几道关于golang并发的面试题
6132. 使数组中所有元素都等于零-快速排序法
@WebServlet注解(Servlet注解)
numpy.around
The Spark of Sql join on the and and where
如何更好的理解的和做好工作?
在CDH的hue上的oozie出现,提交 Coordinator My Schedule 时出错
CDH6的Hue打开出现‘ascii‘ codec can‘t encode characters
Building a cloud-native DevOps environment
Calculate the angle of a line defined by two points
分享一份接口测试项目(非常值得练手)