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 ...
随机推荐
- 2014 项目中用到batik
现在手头上的项目用到batik编程,用的maven管理jar包,要在pom.xml中dependencies标签下添加一下配置(batik编程使用的顶层jar包) <dependency> ...
- HTML&CSS学习心德
学了html&css一周的时间,每天上课9小时,有空就看一下HTML+div+CSS视频,感觉还不错. 基本思路:从大的方面(整体结构)着手,将HTML的基本知识"解构"然 ...
- Android开源控件PhotoView的使用
整体来说,它是一个更高级的ImageView,支持缩放,多点触控缩放,滚动和滑动,单机,长按等事件: PhotoView的git托管地址:https://github.com/chrisbanes/P ...
- RadioButtonFor绑定值
<div class="form-group"> <label class="control-label col-md-2">是否< ...
- 程序员的成长与规划 | 送签名书啦 | StuQ专访foruok
StuQ(InfoQ的朋友)对我做了一次专访,下面是原文. 福利:送一本签名版<你好哇,程序员>,参与方式在文末.
- JS简介
JS是一本广泛应用于浏览器客户端的脚本语言,由netspace公司设计,当时和sun公司合作,所以名字取得像java JS的常见用途:1)HTML DOM操作(节点操作,比如添加.修改.删除节点 ) ...
- ListView到顶部不可再拉
ListView到顶部不可再拉,主要针对魅族.索爱等手机: 如果你的程序针对的是2.3及以上版本,可以直接调用AbsListView的setOverScrollMode方法来解决这个问题. 如果还要兼 ...
- 项目里总结出来的log4j模板
项目日志模板:http://www.cnblogs.com/baibaluo/archive/2011/06/03/2072091.html#commentform #全局设置 log4j.rootL ...
- python3 如何使用ip、爬虫
使用urllib.request.random模块,不说了贴代码 url="*"; iplist=['70.254.226.206:8080'];proxy_support=url ...
- Spring可以将简单的组件配置
这次听了老师的课程,觉得还是需要更加集中的去把各种题进行一个分类吧,然后有针对的去准备,虽然据说这一块在面试中也不容易考到,但是毕竟是难点,还是需要好好准备一下的.因为在dp这个方面,我算是一个比较新 ...