leetcode 473. Matchsticks to Square | 473. 火柴拼正方形(递归)
题目
https://leetcode.com/problems/matchsticks-to-square/
题解
看了 hint 之后,才有思路。
讨论区有个人说得好:
This solution should mention that this problem is an instance of the well-known Bin Packing Problem, which has been proven to be NP-complete, so it is not possible to implement a solution that takes less than exponential time. This would be a very important fact for the candidate to identify, because they otherwise will likely spin their wheels trying to identify a polynomial-time solution.
Once you’ve identified that the problem is NP-complete, you know that you’ll just have to implement a “brute force” solution, and the only task remaining is to look for opportunities to reduce the amount of work you need to do via pruning, memoization, etc.
…
Exactly. In fact second solution is over-engineering because there isn’t an actual improvement in big o time complexity.
If I was the interviewer, if the candidate identified that it’s a variant of bin-packing, therefore NP-complete, and gave a clean backtracking solution, that would be full marks.
class Solution {public boolean makesquare(int[] arr) {Arrays.sort(arr);int sum = 0;for (int m : arr) {sum += m;}if (sum % 4 != 0) return false;return process(arr, arr.length - 1, 0, 0, 0, 0, sum / 4);}public boolean process(int[] arr, int i, int l1, int l2, int l3, int l4, int target) {if (i < 0) return l1 == target && l2 == target && l3 == target && l4 == target;if (l1 > target || l2 > target || l3 > target || l4 > target) return false;return process(arr, i - 1, l1 + arr[i], l2, l3, l4, target) ||process(arr, i - 1, l1, l2 + arr[i], l3, l4, target) ||process(arr, i - 1, l1, l2, l3 + arr[i], l4, target) ||process(arr, i - 1, l1, l2, l3, l4 + arr[i], target);}
}
leetcode 473. Matchsticks to Square | 473. 火柴拼正方形(递归)相关推荐
- Leetcode 473. Matchsticks to Square 卖火柴的小女孩画框框 解题报告
1 解题思想 这道题的意思就是卖火柴的小女孩有一堆长度不等的火柴,他希望拼成一个正方形,不知道可以不可以? 所谓可以就是: 1.用了所有火柴,一根不多,一根不少,当然一根用一次 2.四条边长度一样 所 ...
- LeetCode 473. 火柴拼正方形
473. 火柴拼正方形 还记得童话<卖火柴的小女孩>吗?现在,你知道小女孩有多少根火柴,请找出一种能使用所有火柴拼成一个正方形的方法.不能折断火柴,可以把火柴连接起来,并且每根火柴都要用到 ...
- 经典回溯之火柴拼正方形
473. 火柴拼正方形 给定很多小短火柴,拼成一个正方形 用到的技巧 1. 排序,传参数,起到剪枝的效果,排序后前面搜索过的在下层时直接跳过,具体体现为 for (int i = idx;i< ...
- LeetCode 473. 火柴拼正方形(回溯)
文章目录 1. 题目 2. 解题 1. 题目 还记得童话<卖火柴的小女孩>吗?现在,你知道小女孩有多少根火柴,请找出一种能使用所有火柴拼成一个正方形的方法. 不能折断火柴,可以把火柴连接起 ...
- 【Leetcode刷题Python】473. 火柴拼正方形
1 题目 你将得到一个整数数组 matchsticks ,其中 matchsticks[i] 是第 i 个火柴棒的长度.你要用 所有的火柴棍 拼成一个正方形.你 不能折断 任何一根火柴棒,但你可以把它 ...
- [Swift]LeetCode473. 火柴拼正方形 | Matchsticks to Square
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
- 【473. 火柴拼正方形】
来源:力扣(LeetCode) 描述 你将得到一个整数数组 matchsticks ,其中 matchsticks[i] 是第 i 个火柴棒的长度.你要用 所有的火柴棍 拼成一个正方形.你 不能折断 ...
- 473. 火柴拼正方形
还记得童话<卖火柴的小女孩>吗?现在,你知道小女孩有多少根火柴,请找出一种能使用所有火柴拼成一个正方形的方法.不能折断火柴,可以把火柴连接起来,并且每根火柴都要用到. 输入为小女孩拥有火柴 ...
- 算法----火柴拼正方形
题目 你将得到一个整数数组 matchsticks ,其中 matchsticks[i] 是第 i 个火柴棒的长度.你要用 所有的火柴棍 拼成一个正方形.你 不能折断 任何一根火柴棒,但你可以把它们连 ...
最新文章
- 算法战:需要人工智能生态系统来增强安全性
- Druid.io索引过程分析——时间窗,列存储,LSM树,充分利用内存,concise压缩
- Consul初探-集成ocelot
- 2019年第十届蓝桥杯 C / C ++省赛 B 组真题题解
- 后处理曲线编辑_LSPREPOST后处理
- 报错:非介入式客户端验证规则中的验证类型名称必须唯一。下列验证类型出现重复...
- html手机背景音乐,HTML插入背景音乐方法【全】
- iOS崩溃日志 如何看
- AWG标准线径规格对照表 (2
- BS架构和CS架构的优缺点
- 树莓派4B静态IP与屏幕分辨率设置
- 计算机虚拟化技术论文,虚拟化技术在计算机技术中的运用
- 完美解释:wenet-流式与非流式语音识别统一模型
- VC 获取任务栏窗口的句柄
- OTSU(最大类间方差法、大津算法)
- 电大计算机应用基础期末考试题,电大计算机应用基础模拟题答案.docx
- LINUX-查看历史操作记录
- 面试题一(计算机基础、逻辑)
- 小凯机器人软件_Cruzr-Cruzr(机器人控制软件)下载 v1.5.20190706.48官方版--pc6下载站...
- Android自定义控件开发入门与实战(1)绘图基础