题目

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. 火柴拼正方形(递归)相关推荐

  1. Leetcode 473. Matchsticks to Square 卖火柴的小女孩画框框 解题报告

    1 解题思想 这道题的意思就是卖火柴的小女孩有一堆长度不等的火柴,他希望拼成一个正方形,不知道可以不可以? 所谓可以就是: 1.用了所有火柴,一根不多,一根不少,当然一根用一次 2.四条边长度一样 所 ...

  2. LeetCode 473. 火柴拼正方形

    473. 火柴拼正方形 还记得童话<卖火柴的小女孩>吗?现在,你知道小女孩有多少根火柴,请找出一种能使用所有火柴拼成一个正方形的方法.不能折断火柴,可以把火柴连接起来,并且每根火柴都要用到 ...

  3. 经典回溯之火柴拼正方形

    473. 火柴拼正方形 给定很多小短火柴,拼成一个正方形 用到的技巧 1.  排序,传参数,起到剪枝的效果,排序后前面搜索过的在下层时直接跳过,具体体现为 for (int i = idx;i< ...

  4. LeetCode 473. 火柴拼正方形(回溯)

    文章目录 1. 题目 2. 解题 1. 题目 还记得童话<卖火柴的小女孩>吗?现在,你知道小女孩有多少根火柴,请找出一种能使用所有火柴拼成一个正方形的方法. 不能折断火柴,可以把火柴连接起 ...

  5. 【Leetcode刷题Python】473. 火柴拼正方形

    1 题目 你将得到一个整数数组 matchsticks ,其中 matchsticks[i] 是第 i 个火柴棒的长度.你要用 所有的火柴棍 拼成一个正方形.你 不能折断 任何一根火柴棒,但你可以把它 ...

  6. [Swift]LeetCode473. 火柴拼正方形 | Matchsticks to Square

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  7. 【473. 火柴拼正方形】

    来源:力扣(LeetCode) 描述 你将得到一个整数数组 matchsticks ,其中 matchsticks[i] 是第 i 个火柴棒的长度.你要用 所有的火柴棍 拼成一个正方形.你 不能折断 ...

  8. 473. 火柴拼正方形

    还记得童话<卖火柴的小女孩>吗?现在,你知道小女孩有多少根火柴,请找出一种能使用所有火柴拼成一个正方形的方法.不能折断火柴,可以把火柴连接起来,并且每根火柴都要用到. 输入为小女孩拥有火柴 ...

  9. 算法----火柴拼正方形

    题目 你将得到一个整数数组 matchsticks ,其中 matchsticks[i] 是第 i 个火柴棒的长度.你要用 所有的火柴棍 拼成一个正方形.你 不能折断 任何一根火柴棒,但你可以把它们连 ...

最新文章

  1. 算法战:需要人工智能生态系统来增强安全性
  2. Druid.io索引过程分析——时间窗,列存储,LSM树,充分利用内存,concise压缩
  3. Consul初探-集成ocelot
  4. 2019年第十届蓝桥杯 C / C ++省赛 B 组真题题解
  5. 后处理曲线编辑_LSPREPOST后处理
  6. 报错:非介入式客户端验证规则中的验证类型名称必须唯一。下列验证类型出现重复...
  7. html手机背景音乐,HTML插入背景音乐方法【全】
  8. iOS崩溃日志 如何看
  9. AWG标准线径规格对照表 (2
  10. BS架构和CS架构的优缺点
  11. 树莓派4B静态IP与屏幕分辨率设置
  12. 计算机虚拟化技术论文,虚拟化技术在计算机技术中的运用
  13. 完美解释:wenet-流式与非流式语音识别统一模型
  14. VC 获取任务栏窗口的句柄
  15. OTSU(最大类间方差法、大津算法)
  16. 电大计算机应用基础期末考试题,电大计算机应用基础模拟题答案.docx
  17. LINUX-查看历史操作记录
  18. 面试题一(计算机基础、逻辑)
  19. 小凯机器人软件_Cruzr-Cruzr(机器人控制软件)下载 v1.5.20190706.48官方版--pc6下载站...
  20. Android自定义控件开发入门与实战(1)绘图基础

热门文章

  1. 数据分析与挖掘实战-中医证型关联规则挖掘
  2. SpringBoot跨域请求
  3. 第一个Canvas实例-钟表
  4. CSP前训练错误集锦
  5. 最简单的基于FFMPEG的视频编码器(YUV编码为H.264)
  6. 【Boost】boost库asio详解8——TCP的简单例子1
  7. cocos2d-x初探学习笔记(7)--CCProgressTimer
  8. Ring0和Ring3权限级
  9. #pragma预处理命令
  10. Python识别图片的清晰度