leetcode 473. Matchsticks to Square

本文探讨了如何利用给定长度的火柴棒拼成一个正方形的问题,并提供了三种不同的算法实现方案。通过分析每种方法的特点,展示了如何优化算法以提高运行效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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, and each matchstick must be used exactly one time.

Your input will be several matchsticks the girl has, represented with their stick length. Your output will either be true or false, to represent whether you could make one square using all the matchsticks the little match girl has.
Note:
- The length sum of the given matchsticks is in the range of 0 to 10^9.
- The length of the given matchstick array will not exceed 15.

给你一堆火柴,问能否用所有的火柴拼出一个正方形来。看到数据范围应该是用暴力了。

class Solution {
public:
    void helper(vector<int>& nums, int target, int cur, int level, vector<bool>& vis){
        //cout << cur << " " << level << endl;
        if (cur == target){
            cur = 0;
            ++level;
            helper(nums, target, cur, level, vis);
            return;
        }
        if (level == 4){
            ans = true;
            return;
        }

        int meet = -1;//用来剪枝

        for (int i = 0;i < nums.size();++i){
            if (vis[i]==false && meet!=nums[i] && cur+nums[i]<=target){
                vis[i] = true;
                meet = nums[i];
                helper(nums, target, cur+nums[i], level, vis);
                if (ans) return;
                vis[i] = false;
            }

        }   
    }

    bool makesquare(vector<int>& nums) {
        ans = false;
        if (nums.size() < 4) return false;
        long long sum = 0;
        for (int num: nums){
            sum += num;
        }
        int target = sum / 4;

        cout << sum << " " << target << endl;

        if (sum % target != 0) 
            return false;

        sort(nums.begin(), nums.end(), greater<int>());
        if (nums[0] > target){
            return false;
        }
        vector<bool> vis(nums.size(), false);
        helper(nums, target, 0, 0, vis);
        return ans;
    }
private:
    bool ans;
};

这种做法(做法1)大概是240ms左右。


class Solution {
    bool dfs(vector<int> &sidesLength,const vector<int> &matches, int index, const int target) {
        if (index == matches.size())
            return sidesLength[0] == sidesLength[1] && sidesLength[1] == sidesLength[2] && sidesLength[2] == sidesLength[3];
        for (int i = 0; i < 4; ++i) {
            if (sidesLength[i] + matches[index] > target) // first
                continue;
            // int j = i;
            // while (--j >= 0) // third
            //     if (sidesLength[i] == sidesLength[j]) 
            //         break;
            // if (j != -1) continue;
            sidesLength[i] += matches[index];
            if (dfs(sidesLength, matches, index + 1, target))
                return true;
            sidesLength[i] -= matches[index];
        }
        return false;
    }
public:
    bool makesquare(vector<int>& nums) {
        if (nums.size() < 4) return false;
        int sum = 0;
        for (const int val: nums) {
            sum += val;
        }
        if (sum % 4 != 0) return false;
        sort(nums.begin(), nums.end(), [](const int &l, const int &r){return l > r;}); // second
        vector<int> sidesLength(4, 0);
        return dfs(sidesLength, nums, 0, sum / 4);
    }
};

这种做法(做法2)大概是50ms左右。如果把注释的几行取消注释(做法3),速度竟然来到了7ms,Amazing~
递归算法的优化主要在减少递归的深度和广度。做法2相对于做法1大大减少了深度,做法3相对于做法2像是做了一种启发式求解:尽可能地让四条边相等,如果当前边已经和前面的边相等,则“照顾后面的边”。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值