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 ...
随机推荐
- 码农带你区分String类型的"=="和equals()
大家玩csdn也可以关注我的博客额http://blog.csdn.net/wujiandong25253344 情形一: String s1= new String("hello" ...
- GGXX的卡通渲染实现 真的好变态......
最近在youtube上看了GDC,学了很多东西,最让我震撼的就是ggxx的卡通渲染了.感慨一下,想要用3D做出二次元的效果,真的不容易.现记录一些要点: 1)不要使用normal map来做cel-s ...
- NOIP 考前 数据结构复习
BZOJ 1455 左偏树即可 #include <cstdio> #define LL long long ; struct Info{LL l,r,v,Dis;}Tree[Maxn]; ...
- BackTrack5-r3安装用户组-软件中心
所需文件包地址:http://pan.baidu.com/s/1i3ouc9v(64位更新包) 进入BT系统图形模式-将(用户组-软件中心)文件夹改名(a)并拖进BT系统图形桌面-打开BT终端输入:c ...
- 8个超实用的jQuery技巧攻略
1)禁用右键单击功能 如果你想为用户节省网站信息,那么开发者可以使用这段代码——禁用右键单击功能. <font><font>$(document).ready(function ...
- EL与Velocity基本语法总结:
El(expression language): 基本语法点: $与{}搭配使用是常态取值 . 与[]的区别,后者可以取特殊值:- .等 支持一些基本的逻辑运算: && || > ...
- JS——全选与反选
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Android Studio Eclipse Code Formatter
在从Eclipse转到Android Studio上开发后,如果还想继续使用在Eclipse上制定的自定义的Code Formatter的话,需要按如下步骤操作:1.进入Settings界面,如果能看 ...
- ios常见的页面传值方式
iOS页面间的传值细分有很多种,基本的传值方式有三种:委托Delegate传值.通知NSNotification传值.Block传值,其他在项目中可能会遇到的还有:UserDefault或文件方式传值 ...
- iOS 传值 委托(delegate)和block 对比
技术交流新QQ群:414971585 这篇文章建议和前一篇一起看, 另外先弄清楚IOS的block是神马东东. 委托和block是IOS上实现回调的两种机制.Block基本可以代替委托的功能,而且实 ...