leetcode 473. 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, 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像是做了一种启发式求解:尽可能地让四条边相等,如果当前边已经和前面的边相等,则“照顾后面的边”。

猜你喜欢

转载自blog.csdn.net/guojunxiu/article/details/79703822