알고리즘 공부 2020. 8. 20. 22:18

https://leetcode.com/problems/partition-equal-subset-sum/

 

Partition Equal Subset Sum - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

/**
 * @param {number[]} nums
 * @return {boolean}
 */
var canPartition = function(nums) {    
    const hash = Array(7000).fill(false)
    
    var sum = 0;    
    for( var i=0 ; i<nums.length; ++i )    
        sum += nums[i];    
    sum/=2;    
    
    const find = function( p_idx, p_sum )
    {   
        if( p_sum > sum )   return false;
        
        if( hash[p_sum] )   return false;
        if( !(p_sum-sum) )  return true;
        
        if( p_idx < nums.length )        
            return find( p_idx+1, p_sum+nums[p_idx] ) || find( p_idx+1, p_sum );        
        else
        {
            hash[p_sum] = true;
            return false;        
        }
    }
    return find(0,0)
};
posted by Sense.J
: