当前位置:网站首页>【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;
}
}
时空复杂度一样。
边栏推荐
- Sql之各种Join
- 20220725 Information update
- 中职网络安全竞赛B7比赛部署流程
- Thinkphp 5.0.24变量覆盖漏洞导致RCE分析
- Data Organization --- Chapter 5 Trees and Binary Trees --- The Concept of Binary Trees --- Application Questions
- What can be done to make this SQL into a dangerous SQL?
- 辛普森悖论
- 程序员还差对象?new一个就行了
- 在MySQL登录时出现Access denied for user ‘root‘@‘localhost‘ (using password YES) 拒绝访问问题解决
- 深度学习基础-基于Numpy的循环神经网络(RNN)实现和反向传播训练
猜你喜欢
Work for 5 years, test case design is bad?To look at the big case design summary
Architecture basic concept and nature of architecture
工作5年,测试用例都设计不好?来看看大厂的用例设计总结
[C language advanced] file operation (2)
async和await用法介绍
CDH6的Hue打开出现‘ascii‘ codec can‘t encode characters
6134. Find the closest node to the given two nodes - force double hundred code
月薪12K,蝶变向新,勇往直前—她通过转行测试实现月薪翻倍~
chrome copies the base64 data of an image
在MySQL中使用MD5加密【入门体验】
随机推荐
GIF制作-灰常简单的一键动图工具
Docker搭建Mysql主从复制
ELK log collection
在MySQL登录时出现Access denied for user ‘root‘@‘localhost‘ (using password YES) 拒绝访问问题解决
Chapter 12 End-User Task As Shell Scripts
6134. Find the closest node to the given two nodes - force double hundred code
Get piggy homestay (short-term rental) data
Access the selected node in the console
JAX-based activation function, softmax function and cross entropy function
Enterprise firewall management, what firewall management tools are there?
Flink Yarn Per Job - 提交流程一
Wincc报表教程(SQL数据库的建立,wincc在数据库中保存和查询数据,调用Excel模板把数据保存到指定的位置和打印功能)
Programmer is still short of objects? A new one is enough
企业防护墙管理,有什么防火墙管理工具?
numpy.unique
Use Jenkins for continuous integration, this knowledge point must be mastered
在CDH的hue上的oozie出现,提交 Coordinator My Schedule 时出错
一道golang中关于iota的面试题
Additional Features for Scripting
Solve the port to take up