[LeetCode]473. 火柴拼正方形

75 篇文章 0 订阅

题目

473. 火柴拼正方形

473. 火柴拼正方形
你将得到一个整数数组 matchsticks ,其中 matchsticks[i] 是第 i 个火柴棒的长度。你要用 所有的火柴棍 拼成一个正方形。你 不能折断 任何一根火柴棒,但你可以把它们连在一起,而且每根火柴棒必须 使用一次 。

如果你能使这个正方形,则返回 true ,否则返回 false 。

 

示例 1:



输入: matchsticks = [1,1,2,2,2]
输出: true
解释: 能拼成一个边长为2的正方形,每边两根火柴。
示例 2:

输入: matchsticks = [3,3,3,3,4]
输出: false
解释: 不能用所有火柴拼成一个正方形。
 

提示:

1 <= matchsticks.length <= 15
1 <= matchsticks[i] <= 108

解法

方法1:回溯

boolean[] vis;
int[] matchsticks;
int maxLen;
int n;

public boolean makesquare(int[] matchsticks) {
    n = matchsticks.length;
    int sum = 0;
    for (int x : matchsticks) sum += x;
    if (sum % 4 != 0) return false;
    maxLen = sum / 4;//正方形边长
    vis = new boolean[n];//每个火柴的访问情况
    this.matchsticks = matchsticks;
    return dfs(0, 0, 0);
}

//squareIndex:当前处理到的边的编号,从0~3 index:当前处理的matchsticks的哪根火柴,curLen 当前这条边所积累的边长
public boolean dfs(int squareIndex, int index, int curLen) {
    if (squareIndex == 4) return true;
    if (curLen == maxLen) {
        return dfs(squareIndex + 1, 0, 0);
    }
    //遍历每一根火柴
    for (int i = index; i < n; i++) {
        //当前的火车没有被使用过且当前的边长+即将被使用的火柴的长度,不会越界,这根火柴可以被使用
        if (!vis[i] && curLen + matchsticks[i] <= maxLen) {
            vis[i] = true;//标记
            //如果当前的火柴可以使用,那就进入到下一根火车
            if (dfs(squareIndex, i + 1, curLen + matchsticks[i])) {
                return true;
            }
            vis[i] = false;//回溯
        }
    }
    return false;
}
  • 另,逆序排序写法
int n;
int[] matchsticks;
int maxLen;

public boolean makesquare(int[] matchsticks) {
    n = matchsticks.length;
    int sum = 0;
    for (int x : matchsticks) sum += x;
    if (sum % 4 != 0) return false;
    this.matchsticks = matchsticks;
    maxLen = sum / 4;//正方形边长
    int[] sides = new int[4];//4条边的长度
    //逆序排序,不排序会超时
    this.matchsticks = IntStream.of(matchsticks)          // 变为 IntStream
            .boxed()           // 装盒变为 Stream<Integer>
            .sorted(Comparator.reverseOrder()) // 按自然序相反排序
            .mapToInt(Integer::intValue)       // 变为 IntStream
            .toArray();        // 又变回 int[]
    return dfs(0, sides);
}

public boolean dfs(int index, int[] sides) {
    if (index == matchsticks.length) {
        return true;
    }
    for (int i = 0; i < sides.length; i++) {
        sides[i] += matchsticks[index];
        if (sides[i] <= maxLen && dfs(index + 1, sides)) {
            return true;
        }
        sides[i] -= matchsticks[index];
    }
    return false;
}
int n;
int[] matchsticks;
int maxLen;

public boolean makesquare(int[] matchsticks) {
    n = matchsticks.length;
    int sum = 0;
    for (int x : matchsticks) sum += x;
    if (sum % 4 != 0) return false;
    this.matchsticks = matchsticks;
    maxLen = sum / 4;//正方形边长
    int[] sides = new int[4];//4条边的长度
    //逆序排序,不排序会超时
    this.matchsticks = IntStream.of(matchsticks)          // 变为 IntStream
            .boxed()           // 装盒变为 Stream<Integer>
            .sorted(Comparator.reverseOrder()) // 按自然序相反排序
            .mapToInt(Integer::intValue)       // 变为 IntStream
            .toArray();        // 又变回 int[]
    return dfs(0, sides);
}

private boolean dfs(int index, int[] sides) {
    if (index >= matchsticks.length) {
        if (sides[0] == maxLen && sides[1] == maxLen && sides[2] == maxLen) {
            return true;
        }
    }
    for (int i = 0; i < sides.length; i++) {
        if (sides[i] + matchsticks[index] > maxLen) {
            continue;
        }
        sides[i] += matchsticks[index];
        if (dfs(index + 1, sides)) {
            return true;
        }
        sides[i] -= matchsticks[index];
    }
    return false;
}

方法2:动态规划+状态压缩

思路来自官解

      public boolean makesquare(int[] matchsticks) {
            int totalLen = Arrays.stream(matchsticks).sum();
            if (totalLen % 4 != 0) {
                return false;
            }
            int len = totalLen / 4, n = matchsticks.length;
            int[] dp = new int[1 << n];
            Arrays.fill(dp, -1);
            dp[0] = 0;
            for (int s = 1; s < (1 << n); s++) {
                for (int k = 0; k < n; k++) {
                    if ((s & (1 << k)) == 0) {
                        continue;
                    }
                    int s1 = s & ~(1 << k);
                    if (dp[s1] >= 0 && dp[s1] + matchsticks[k] <= len) {
                        dp[s] = (dp[s1] + matchsticks[k]) % len;
                        break;
                    }
                }
            }
            return dp[(1 << n) - 1] == 0;
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值