【LeetCode】473. Matchsticks to Square 解题报告(Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/matchsticks-to-square/description/
题目描述
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: true
Explanation: 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: false
Explanation: 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 0 to 10^9.
- The length of the given matchstick array will not exceed 15.
题目大意
题目很长,讲的是卖火孩的小女柴,把火柴拼在一起能不能得到一个正方形。当然火柴是不能折断的,而且要全部都用上。
解题方法
回溯法
这个题目很长,其实就一句话:能不能把一组数字分成4组,每组的和是相同的。
我们注意到题目给出的数组长度最多只有15个,基本上可以使用O(N!)的时间复杂度去解决。所以我们可以直接使用回溯法。回溯的思路是先设置好4条边,然后把每一个火柴看看能不能放到4条边中的一个去,如果可以的话就继续向后扫描,直到所有的火柴全部用上为止。
同样使用416. Partition Equal Subset Sum的方法,也就是在使用记录四组的和的方式,进行遍历的时候保存各个组的和,如果不能满足就把这个数字再加上,相当于跳过这个数字的方式。最后结束的条件就是所有的数字全部都用完了,因为如果用完了,说明我们把所有的火柴都放到了4条边中的一个,所以得到每组都满足我们条件的结论。
Python代码:
class Solution:
def makesquare(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
if not nums or len(nums) < 4: return False
_sum = sum(nums)
div, mod = divmod(_sum, 4)
if mod != 0 or max(nums) > _sum / 4: return False
nums.sort(reverse = True)
target = [div] * 4
return self.dfs(nums, 0, target)
def dfs(self, nums, index, target):
if index == len(nums): return True
num = nums[index]
for i in range(4):
if target[i] >= num:
target[i] -= num
if self.dfs(nums, index + 1, target): return True
target[i] += num
return False
C++代码如下:
class Solution {
public:
bool makesquare(vector<int>& nums) {
if (nums.size() < 4) return false;
int sum = accumulate(nums.begin(), nums.end(), 0);
if (sum % 4 != 0)
return false;
int edge = sum / 4;
vector<int> target(4, edge);
return helper(nums, 0, target);
}
bool helper(vector<int>& nums, int index, vector<int>& target) {
const int N = nums.size();
if (index == N) return true;
int num = nums[index];
for (int i = 0; i < 4; ++i) {
if (target[i] >= num) {
target[i] -= num;
if (helper(nums, index + 1, target))
return true;
target[i] += num;
}
}
return false;
}
};
日期
2018 年 4 月 2 日 —— 要开始准备ACM了
2019 年 2 月 23 日 —— 没时间了
【LeetCode】473. Matchsticks to Square 解题报告(Python & C++)的更多相关文章
- 【LeetCode】593. Valid Square 解题报告(Python)
[LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】221. Maximal Square 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址: https://leet ...
- 【LeetCode】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- LeetCode "473. Matchsticks to Square"
A trickier DFS, with a little bit complex recursion param tweak, and what's more important is prunin ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】649. Dota2 Senate 解题报告(Python)
[LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】911. Online Election 解题报告(Python)
[LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
- 【LeetCode】886. Possible Bipartition 解题报告(Python)
[LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
随机推荐
- 用原生CSS编写动态字体
HTML部分: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <t ...
- Levenshtein莱文斯坦算法在项目中的应用
简介 根据维基百科的描述,在信息理论.语言学和计算机科学中,莱文斯坦距离是一个测量两个序列之间差异的字符串度量.非正式地,两个单词之间的莱文斯坦距离是将一个单词改变为另一个单词所需的最小单字符编辑次数 ...
- 单体内置对象 Global 和 Math
单体内置对象 Global 和 Math 在所有代码执行前,作用域中就已经存在两个内置对象:Global(全局)和Math.在大多数ES实现中都不能直接访问Global对象.不过,WEB浏览器实现了承 ...
- android studio 使用 aidl(一)基础用法
最近公司需要开发一个项目用的到aidl,之前研究过eclipse版本的,但是好久了一直没用,现在需要捡起来,但是现在都用android studio了,所以查了下资料 都不是很全,我在这里总结一下,方 ...
- ehcache详解
Ehcache是现在最流行的纯Java开 源缓存框架,配置简单.结构清晰.功能强大,最初知道它,是从Hibernate的缓存开始的.网上中文的EhCache材料以简单介绍和配置方法居多, 如果你有这方 ...
- sftp 上传下载 命令介绍
sftp是Secure FileTransferProtocol的缩写,安全文件传送协议.可以为传输文件提供一种安全的加密方法. sftp与 ftp有着几乎一样的语法和功能.SFTP为 SSH的一部分 ...
- logstash 正则表达式
正则表达式 3. 使用给定好的符号去表示某个含义 4. 例如.代表任意字符 5. 正则符号当普通符号使用需要加反斜杠 正则的发展 6. 普通正则表达式 7. 扩展正则表达式 普通正则表达式 . 任意一 ...
- Springboot整合MongoDB(Eclipse版本)
IDEA版本其实也差不多的,大同小异 简单Demo地址: https://blog.csdn.net/shirukai/article/details/82152243 Springboot项目整合M ...
- Docker从入门到精通(三)——概念与执行流程
前面我们大概介绍了docker是什么以及如何安装docker,但是对里面出现的一些名词,可能大家还不熟悉,这篇文章就来为大家解惑. 1.容器化平台 Docker 是提供应用打包,部署与运行应用的容器化 ...
- Tableau如何绘制双柱折线组合图
一.数据准备如下所示 二.将日期拖拽至列,销售额拖拽至行,结果如下所示 三.右键日期排序-选择手动排序 四.将指标拖拽至标记卡上 五.创建计算字段增长率 SUM(IF YEAR(日期)=2017 th ...