Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

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;
        
    }
    
}
Comment

PREVIOUS NEXT
Code Example
Java :: How to find the Levenshtein distance between two strings of characters using dynamic programming, in Java? 
Java :: show confirmation dialog java android 
Java :: character to lower java 
Java :: java print type of object 
Java :: java get keys from tree map 
Java :: how to converet negative byte value to postive int value in java 
Java :: how to change color of radio button in android 
Java :: java one line if else 
Java :: firestore add to field array 
Java :: system.out.write in java example 
Java :: check if object in array java 
Java :: why java is popular 
Java :: RealmConfiguration.Builder.allowWritesOnUiThread(true) 
Java :: primefaces custom validate 
Java :: circle line intersection java 
Java :: show input dialog java 
Java :: java coding problems 
Java :: how to get float value in json java 
Java :: how to remove components from a JFRame java 
Java :: java toast 
Java :: java random seed 
Java :: java array loop backwards 
Java :: java date time 
Java :: java 8 group a collections by 2 property 
Java :: switch activity android studi 
Java :: Display double in decimal places java 
Java :: android studio textbox change text 
Java :: Java program to find the sum of first 100 numbers 
Java :: display an image java 
Java :: java find longest string in list 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =