Leetcode 473.火柴拼正方形

火柴拼正方形

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

输入为小女孩拥有火柴的数目,每根火柴用其长度表示。输出即为是否能用所有的火柴拼成正方形。

示例 1:

输入: [1,1,2,2,2]

输出: true

解释: 能拼成一个边长为2的正方形,每边两根火柴。

示例 2:

输入: [3,3,3,3,4]

输出: false

解释: 不能用所有火柴拼成一个正方形。

注意:

  1. 给定的火柴长度和在 0 到 10^9之间。
  2. 火柴数组的长度不超过15。

想象正方形的4条边是4个桶,将每个火柴棍回溯放置在每个桶中,放完N个后,检查4个桶中的长度和是否相同

优化剪枝:

1.N个火柴棍的总和对4取余是不是0,不是的话返回假

2.长度按照从大到小排序,先尝试长的,减少回溯的可能

3.每次放置时,每条边上不可放置超过总和1/4长度的火柴棍

 1 import java.util.Arrays;
 2
 3 class Solution {
 4     public boolean makesquare(int[] nums) {
 5         if(nums.length<4) return false;
 6         int sum=0;
 7         for(int i=0;i<nums.length;i++) sum+=nums[i];
 8         if(sum%4!=0) return false;
 9         Arrays.sort(nums);
10         int[] bucket=new int[4];
11         return generate(0,nums,sum/4,bucket);
12     }
13
14     public boolean generate(int i,int[] nums,int target,int[] bucket){
15         if(i==nums.length) return bucket[0]==target&&bucket[1]==target&&bucket[2]==target&&bucket[3]==target;
16         for(int j=0;j<4;j++){
17             if(bucket[j]+nums[i]>target) continue;
18             bucket[j]+=nums[i];
19             if(generate(i+1,nums,target,bucket)) return true;
20             bucket[j]-=nums[i];
21         }
22         return false;
23     }
24 }

原文地址:https://www.cnblogs.com/kexinxin/p/10280234.html

时间: 2024-07-31 14:58:29

Leetcode 473.火柴拼正方形的相关文章

473. 火柴拼正方形

1 class Solution 2 { 3 public: 4 bool makesquare(vector<int>& nums) 5 { 6 if(nums.size()<4) return false; 7 int sum=0; 8 for(int i=0;i<nums.size();i++) sum+=nums[i]; 9 if(sum%4 != 0) return false; 10 sort(nums.rbegin(),nums.rend());//最神奇的一

leetcode 火柴拼正方形 深搜

还记得童话<卖火柴的小女孩>吗?现在,你知道小女孩有多少根火柴,请找出一种能使用所有火柴拼成一个正方形的方法.不能折断火柴,可以把火柴连接起来,并且每根火柴都要用到. 输入为小女孩拥有火柴的数目,每根火柴用其长度表示.输出即为是否能用所有的火柴拼成正方形. 示例?1: 输入: [1,1,2,2,2] 输出: true 解释: 能拼成一个边长为2的正方形,每边两根火柴. 示例?2: 输入: [3,3,3,3,4] 输出: false 解释: 不能用所有火柴拼成一个正方形. 来源:力扣(LeetC

[Swift]LeetCode473. 火柴拼正方形 | Matchsticks to Square

Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match girl has, please find out a way you can make one square by using up all those matchsticks. You should not break any stick, but you can link them up, a

LeetCode 473 - Matchsticks to Square - Medium (Python)

Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match girl has, please find out a way you can make one square by using up all those matchsticks. You should not break any stick, but you can link them up, a

Leetcode 221.最大的正方形

最大的正方形 在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积. 示例: 输入: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 输出: 4 判断以某个点为正方形右下角时最大的正方形时,那它的上方,左方和左上方三个点也一定是某个正方形的右下角,否则该点为右下角的正方形最大就是它自己了.这是定性的判断,那具体的最大正方形边长呢?我们知道,该点为右下角的正方形的最大边长,最多比它的上方,左方和左上方为右下角的正方形的边长多1,最好的情

[LeetCode]最大系列(最大正方形221,最大加号标志764)

221. 最大正方形 题目描述: 在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积. 示例: 输入: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 输出: 4 思路: 这道题是动态规划,所以我们要找到动态方程 dp[i][j] = min(dp[i-1][j],dp[i][j-1],dp[i-1][j-1])+1 举个例子 1 0 1 0 0 1 0 1 1 1 1 1 1 2 2 1 0 0 1 0 代码: class Sol

[LeetCode] Valid Square 验证正方形

Given the coordinates of four points in 2D space, return whether the four points could construct a square. The coordinate (x,y) of a point is represented by an integer array with two integers. Example: Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 =

Leetcode题解 - 树、DFS部分简单题目代码+思路(700、671、653、965、547、473、46)

700. 二叉搜索树中的搜索 - 树 给定二叉搜索树(BST)的根节点和一个值. 你需要在BST中找到节点值等于给定值的节点. 返回以该节点为根的子树. 如果节点不存在,则返回 NULL. 思路: 二叉搜索树的特点为左比根小,右比根大.那么目标结点就有三种可能: 1. 和根一样大,那么直接返回根即可. 2. 比根的值小,那么应该再去次左子树中搜索. 3. 比根的值大,那么应该再次去右子树中搜索. 可以看到这就是一个递归的思路. class Solution: def searchBST(self

leet

# 题名1 两数之和    2 两数相加    3 无重复字符的最长子串    4 寻找两个有序数组的中位数    5 最长回文子串    6 Z 字形变换    7 整数反转    8 字符串转换整数 (atoi)    9 回文数    10 正则表达式匹配    11 盛最多水的容器    12 整数转罗马数字    13 罗马数字转整数    14 最长公共前缀    15 三数之和    16 最接近的三数之和    17 电话号码的字母组合    18 四数之和    19 删除链表