Java dp solution easy to understand


  • 0
    C
    public int integerBreak(int n) {
        if(n == 2){
            return 1;
        }
        
        int[] table = new int[n + 1];
        table[1] = 0;
        table[2] = 1;
        
        for(int i = 3; i <= n; i++){
            
            for(int j = 2; j < i; j++){
                table[i] = Math.max(table[i], table[j] * (i - j));
                table[i] = Math.max(table[i], j * (i - j));
            }
        }
        
        return table[n];
    }

Log in to reply
 

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