LeetCode "473. Matchsticks to Square"
A trickier DFS, with a little bit complex recursion param tweak, and what's more important is pruning strategies.. or your code will TLE.
- class Solution {
- bool go(vector<bool> &m, int edge, int eleft, int etgt, vector<int> &in, int taken)
- {
- if(edge > ) return false;
- if(edge == && eleft == etgt && in.size() == taken)
- {
- return true;
- }
- int last = -;
- for(int i = in.size()-; i >=; i --)
- {
- if(m[i]) continue;
- if(in[i] > eleft) continue; // because it is sorted
- if(in[i] == last) continue; // important: critical pruning.
- if(in[i] <= eleft)
- {
- m[i] = true;
- bool fit = in[i] == eleft;
- if(go(m, edge + (fit?:), fit?etgt:(eleft-in[i]), etgt, in, taken + ))
- {
- return true;
- }
- m[i] = false;
- last = in[i];
- }
- }
- return false;
- }
- public:
- bool makesquare(vector<int>& nums) {
- if(nums.size() < ) return false;
- int sum = accumulate(nums.begin(), nums.end(), );
- if(!sum || (sum % )) return false;
- sort(nums.begin(), nums.end());
- if (nums.back() > (sum/)) return false;
- vector<bool> m(nums.size(), false);
- int tgt = sum / ;
- return go(m, , tgt, tgt, nums, );
- }
- };
LeetCode "473. Matchsticks to Square"的更多相关文章
- 【LeetCode】473. Matchsticks to Square 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- 【leetcode】473. Matchsticks to Square
题目如下: 解题思路:居然把卖火柴的小女孩都搬出来了.题目的意思是输入一个数组,判断能否把数组分成四个子数组,使得每个子数组的和相等.首先我们可以很容易的求出每个子数组的和应该是avg = sum(n ...
- 473. Matchsticks to Square
Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...
- 473 Matchsticks to Square 火柴拼正方形
还记得童话<卖火柴的小女孩>吗?现在,你知道小女孩有多少根火柴,请找出一种能使用所有火柴拼成一个正方形的方法.不能折断火柴,可以把火柴连接起来,并且每根火柴都要用到.输入为小女孩拥有火柴的 ...
- Leetcode之深度优先搜索(DFS)专题-473. 火柴拼正方形(Matchsticks to Square)
Leetcode之深度优先搜索(DFS)专题-473. 火柴拼正方形(Matchsticks to Square) 深度优先搜索的解题详细介绍,点击 还记得童话<卖火柴的小女孩>吗?现在, ...
- LeetCode 题解 593. Valid Square (Medium)
LeetCode 题解 593. Valid Square (Medium) 判断给定的四个点,是否可以组成一个正方形 https://leetcode.com/problems/valid-squa ...
- 【LeetCode】593. Valid Square 解题报告(Python)
[LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- [LeetCode] Matchsticks to Square 火柴棍组成正方形
Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...
- Leetcode: Matchsticks to Square && Grammar: reverse an primative array
Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...
随机推荐
- 将NSString转换成UTF8编码的NSString
在使用网络地址时,一般要先将url进行encode成UTF8格式的编码,否则在使用时可能报告网址不存在的错误,这时就需要进行转换 下面就是转换函数: NSString *urlString= [NSS ...
- Windows多网卡上网设置
http://blog.tianya.cn/blogger/post_show.asp?BlogID=1566293&PostID=12984307
- IT之梦
生活原本不孤单,可惜人们总爱感叹 日子其实很简单,而我却不甘平淡 工作中我尽情的挥洒热汗,只为让成功离自己更近一点 生活中我为人和善,只为人生的路上多一个伙伴 时间不等人,IT尚遥远 我愿舍身敬业,誓 ...
- 估计PI——OpenCV&Cpp
来源:<Learning Image Processing With OpenCV> 算法原理:蒙特卡洛 PI的计算公式: Cpp代码: #include <opencv2/open ...
- Centos安装完MariaDB后启动不了 MySQL is not running, but lock file (/var/lock/subsys/mysql) exists
[root@admin-node subsys]# service mysql startStarting MySQL. ERROR! [root@admin-node subsys]# servic ...
- easyUI中datetimebox和combobox的取值方法
easyUi页面布局中,查询条件放在JS中,如下 <script type="text/javascript"> var columnList = [ [ { ...
- array_unshift() 、
定义和用法 array_unshift() 函数在数组开头插入一个或多个元素. 被加上的元素作为一个整体添加,这些元素在数组中的顺序和在参数中的顺序一样. 该函数会返回数组中元素的个数. 语法 arr ...
- 转发 java数据结构之hashMap详解
概要 这一章,我们对HashMap进行学习.我们先对HashMap有个整体认识,然后再学习它的源码,最后再通过实例来学会使用HashMap.内容包括:第1部分 HashMap介绍第2部分 HashMa ...
- Python 中的map和reduce学习笔记
map和reduce都是Python中的内置函数 map函数接受两个参数,第一个参数是函数,第二个参数是列表,将函数依次作用于列表中的元素,并返回一个元素 reduce同样以函数和列表作为参数,区别在 ...
- SQL server2008-对象资源管理器
对象资源管理器:数据库 .安全性.服务器对象.复制.管理 .SQL server代理 六部分组成