C++ DP Solution


  • 0
    J

    '''

    bool canPartition(vector<int>& nums) {
        int sum = 0;
        for (int x : nums) sum += x;
        if (sum & 1) return false;
        int n = sum >> 1;
        vector<bool> can(n + 1, false);
        can[0] = true;
        
        for (int x : nums)
        {
            for (int j = n; j >= x; j--)
            {
                can[j] = can[j] | can[j - x];
            }
        }
        
        return can[n];
    }
    

    '''


Log in to reply
 

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