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. windows核心编程---第九章 同步设备IO与异步设备IO之同步IO

    同步设备IO 所谓同步IO是指线程在发起IO请求后会被挂起,IO完成后继续执行. 异步IO是指:线程发起IO请求后并不会挂起而是继续执行.IO完毕后会得到设备的通知.而IO完成端口就是实现这种通知的很 ...

  2. [转] js深拷贝和浅拷贝

    一.数组的深浅拷贝 在使用JavaScript对数组进行操作的时候,我们经常需要将数组进行备份,事实证明如果只是简单的将它赋予其他变量,那么我们只要更改其中的任何一个,然后其他的也会跟着改变,这就导致 ...

  3. 手机web页面开发-第一弹

    毕业设计题目<基于three.js的太阳系全景漫游>,项目开发运行在手机端,开始学习手机端页面开发. 新建index.html,写meta标签.meta标签分为两大部分:http标题信息( ...

  4. Linux 防火墙开放特定端口 (iptables)

    iptables是linux下的防火墙,同时也是服务名称.   service  iptables  status        查看防火墙状态 service  iptables  start   ...

  5. NOPI Excel插件导入导出 图片批注

    dateTable导出到excel的MemoryStream /// <summary> /// DataTable导出到Excel的MemoryStream Export() /// & ...

  6. Azure Media Service

    该视频来源于Build 2015, 视频比较老, 从演讲的角度看, 是个非常不错的演讲, 内容也很全面. Apr 27, 2015

  7. 通过git在github上建立gh-pages分支并查看网页效果

    建立gh-pages分支:    进入到你想要上传的文件夹下:           cd text(text为文件夹名)           git初始化           git init   创 ...

  8. MVC+UnitOfWork+Repository+EF 之我见

    UnitOfWork+Repository模式简介: 每次提交数据库都会打开一个连接,造成结果是:多个连接无法共用一个数据库级别的事务,也就无法保证数据的原子性.一致性.解决办法是:在Reposito ...

  9. LPTHW 笨办法学python 33章

    32-33章节 将for-loop和while-loop循环的. 俩种句法就不说了.简单说下个人对于for和while的理解. 我觉得他learn python the hard way这里的写法是, ...

  10. Nginx下css的链接问题

    放在 Nginx 下的网页代码,在链接外部 css 文件时,可能出现没有链接成功的问题.需要在 nginx.conf 里的 http 下添加一行. http { include mime.types;