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"的更多相关文章

  1. 【LeetCode】473. Matchsticks to Square 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...

  2. 【leetcode】473. Matchsticks to Square

    题目如下: 解题思路:居然把卖火柴的小女孩都搬出来了.题目的意思是输入一个数组,判断能否把数组分成四个子数组,使得每个子数组的和相等.首先我们可以很容易的求出每个子数组的和应该是avg = sum(n ...

  3. 473. Matchsticks to Square

    Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...

  4. 473 Matchsticks to Square 火柴拼正方形

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

  5. Leetcode之深度优先搜索(DFS)专题-473. 火柴拼正方形(Matchsticks to Square)

    Leetcode之深度优先搜索(DFS)专题-473. 火柴拼正方形(Matchsticks to Square) 深度优先搜索的解题详细介绍,点击 还记得童话<卖火柴的小女孩>吗?现在, ...

  6. LeetCode 题解 593. Valid Square (Medium)

    LeetCode 题解 593. Valid Square (Medium) 判断给定的四个点,是否可以组成一个正方形 https://leetcode.com/problems/valid-squa ...

  7. 【LeetCode】593. Valid Square 解题报告(Python)

    [LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  8. [LeetCode] Matchsticks to Square 火柴棍组成正方形

    Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...

  9. 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 ...

随机推荐

  1. flyby function

    x=linspace(0.001, 3, 300); y=besselj(5,sqrt(1+x.^2));m=exp(5*i*atan(x.^-1));z=y.*m;plot(x,log(z),'r' ...

  2. spring结合quartz的定时的2种方式

    1. Spring 的org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean类,使用此方法使开发人员对Quar ...

  3. "****" is not translated in zh, zh_CN.的解决方法

    最近在开发一个app,要用到静默安装等一些小技术,但是引发了问题如下: 在Android SDK Tool r19之后, Export的时候遇到xxx is not translated in yyy ...

  4. web api 初体验之 GET和POST传参

    上一篇我们讲到了web api跨域的问题 它几乎是每一个用web api的人都需要去解决的问题,不然都没法测试.接下来会遇到的问题就是传参了.还是用js前台调用服务的方式. GET 方式 get方式传 ...

  5. 移动销售端app的需求分析

    随着网络时代的发展,人们对于网络的依赖越来越大,网上购物便成了一个很大的消费者市场.. 如何分析一个综合的移动销售端app的需求我认为首先要确定用户,从用户的角度一个一个功能过,评估每一个功能的需求, ...

  6. 8个超实用的jQuery技巧攻略

    1)禁用右键单击功能 如果你想为用户节省网站信息,那么开发者可以使用这段代码——禁用右键单击功能. <font><font>$(document).ready(function ...

  7. C++学习笔记33:泛型编程拓展2

    调用标准模板库的find()函数查找数组元素 例子: #include <iostream> #include <algorithm> using namespace std; ...

  8. linux sed和awk的区别

    awkawk是一种程序语言,对文档资料的处理具有很强的功能.awk擅长从格式化报文或从一个大的文本文件中抽取数据.awk的命令格式为:awk [-F filed-separator] “command ...

  9. Spike Notes on Theory of (Software) Transactional Memory[Doing]

    Motivation 程序员是否需要在处理一致性问题或者同步时,一定要make hands dirty?能不能专注于应用级原子性,而无需考虑低层操作系统.运行时支持的原子性概念或者语言构造? 软件事务 ...

  10. CentOS 6 minimal 网络配置

    安装CentOS6 minimal 之后ifconfig 只有lo本地 的127.0.0.1 没有局域网ip. 这边我用的是vmware  nat  DHCP 提供网络服务.过程就不赘述了. 解决笔记 ...