(M)DFS:473. Matchsticks to Square
来源:互联网 发布:网络硬盘源码 编辑:程序博客网 时间:2024/06/06 23:35
这道题实际上是求一个数组能否分成四个和相等的子数组。思路是定义四个组(vector),遍历这个大数组,看nums[i]加在哪一个组中,加在这个组中,对下一个数字进行递归,如果返回false,则说明加在这个组中无解,就在这个组中减掉,看能不能加到下一个组中。
大神分析:
这道题让我们用数组中的数字来摆出一个正方形。跟之前有道题Partition Equal Subset Sum有点像,那道题问我们能不能将一个数组分成和相等的两个子数组,而这道题实际上是让我们将一个数组分成四个和相等的子数组。我一开始尝试着用那题的解法来做,首先来判断数组之和是否是4的倍数,然后还是找能否分成和相等的两个子数组,但是在遍历的时候加上判断如果数组中某一个数字大于一条边的长度时返回false。最后我们同时检查dp数组中一条边长度位置上的值跟两倍多一条边长度位置上的值是否为true,这种方法不幸TLE了。所以只能上论坛求助各路大神了,发现了可以用优化过的递归来解,递归的方法基本上等于brute force,但是C++版本的直接递归没法通过OJ,而是要先给数组从大到小的顺序排序,这样大的数字先加,如果超过target了,就直接跳过了后面的再次调用递归的操作,效率会提高不少,所以会通过OJ。下面来看代码,我们建立一个长度为4的数组sums来保存每个边的长度和,我们希望每条边都等于target,数组总和的四分之一。然后我们遍历sums中的每条边,我们判断如果加上数组中的当前数字大于target,那么我们跳过,如果没有,我们就加上这个数字,然后对数组中下一个位置调用递归,如果返回为真,我们返回true,否则我们再从sums中对应位置将这个数字减去继续循环,参见代码如下:
class Solution {public: bool makesquare(vector<int>& nums) { if (nums.empty() || nums.size() < 4) return false; int sum = accumulate(nums.begin(), nums.end(), 0); if (sum % 4 != 0) return false; vector<int> sums(4, 0); sort(nums.rbegin(), nums.rend()); return helper(nums, sums, 0, sum / 4); } bool helper(vector<int>& nums, vector<int>& sums, int pos, int target) { if (pos >= nums.size()) { return sums[0] == target && sums[1] == target && sums[2] == target; } for (int i = 0; i < 4; ++i) { if (sums[i] + nums[pos] > target) continue; sums[i] += nums[pos]; if (helper(nums, sums, pos + 1, target)) return true; sums[i] -= nums[pos]; } return false; }};
- (M)DFS:473. Matchsticks to Square
- 473. Matchsticks to Square
- 473. Matchsticks to Square**
- 473. Matchsticks to Square
- 473. Matchsticks to Square
- 473. Matchsticks to Square
- 473. Matchsticks to Square
- 473. Matchsticks to Square
- 473. Matchsticks to Square
- 473. Matchsticks to Square
- 473. Matchsticks to Square
- LeetCode 473. Matchsticks to Square
- LeetCode 473. Matchsticks to Square
- [leetcode] 473. Matchsticks to Square
- Leetcode-473. Matchsticks to Square
- Leetcode 473. Matchsticks to Square
- [LeetCode]473. Matchsticks to Square
- [leetcode]473. Matchsticks to Square
- python学习:程序解决方案
- 看完这个,css中position的绝对定位相对定位就懂了
- 节点1检查
- HttpSession对象的详解与实战
- 一道关于索引的使用和key_len的计算的面试题
- (M)DFS:473. Matchsticks to Square
- 常见数据库分页实现
- git 笔记
- Binary Tree Preorder Traversal--LeetCode
- 指向变量的常指针与指向常变量的指针变量的区别
- Java基础
- 八大排序算法之(四)选择排序
- ARUCO marker的解释
- XMLHttpRequest对象