Horje
leetcode 416 Code Example
leetcode 416
Using memoization
class Solution {
    public boolean canPartition(int[] nums) {
        
        Arrays.sort(nums);
        int total = 0;
        
        for(int num:nums)
        total+=num;
        
        
        if(total%2!=0)
        return false;
        
        return canPartition(nums,0,0,total, new HashMap<String, Boolean>());
        
    }
    
    public boolean canPartition(int[] nums, int index,  int sum , int total , HashMap<String,Boolean> state)
    {
        String current = index + "" + sum;
        
        if(state.containsKey(current))
            return state.get(current);
        
        if(sum*2==total)
            return true;
        
        if(sum > total || index >= nums.length)
            return false;
        
        boolean result =  canPartition(nums, index+1 , sum , total,state) ||  canPartition(nums,index+1,sum+nums[index],total,state );
        
        state.put(current, result);
        
        return result;
        
    }
    
}




Java

Related
30/8 Code Example 30/8 Code Example
how to register event spiot Code Example how to register event spiot Code Example
javafx tableview add data Code Example javafx tableview add data Code Example
jaxb exclude field Code Example jaxb exclude field Code Example
shakescleare merchant of venice Code Example shakescleare merchant of venice Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
7