Why I am getting "Wrong" on a testcase but running it in isolation is correct?


  • 0
    B

    Here is the case
    [10,6,5,5,5,3,3,3,2,2,2,2]

    My code is copying the dfs code:

    class Solution {
        class sort_op {
        public:
            bool operator()(int x, int y) {
                return y < x;
            }
        };
    public:
        bool makesquare(vector<int>& nums) {
            if (nums.size() == 0) return false;
            int total = 0;
            for (int i = 0; i < nums.size(); i++) {
                total += nums[i];
            }
            if (total%4 != 0) return false;
            int side = total/4;
            sort(nums.begin(), nums.end());
            vector<int> sums(4, 0);
            return find(nums, side, 0, sums);
        }
        bool find(const vector<int>& nums, const int& target, int index, vector<int>& sums) {
            if (index < 0 || index > nums.size()) return false;
            if (nums[index] > target) return false;
            if (index == nums.size()) {
                if (sums[0] == target && sums[1] == target &&
                    sums[2] == target && sums[3] == target)
                    return true;
                return false;
            }
            for (int i = 0; i < 4; i++) {
                if (sums[i] + nums[index] > target) continue;
                sums[i] += nums[index];
                if (find(nums, target, index+1, sums)) return true;
                sums[i] -= nums[index];
            }
            return false;
        }
    };
    

    It runs and show the result is False when I submitted the code but it runs ok when I am testing it.
    Please help


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.