343. Integer Break

dp[i]=Math.max(dp[i], j*Math.max(i-j,dp[i-j]));

class Solution {
    public int integerBreak(int n) {
        // value at index i refer to the ans of number i;
        int[] memo = new int[n+1];
        memo[1]=1;
        for (int i=2; i<=n; i++){
            for (int j = 1; j<=i/2; j++){
                // compare i-j with memo[i-j];
                memo[i]=Math.max(memo[i], j*Math.max(i-j, memo[i-j]));
            }
        }
        
        return memo[n];
    }
}

Last updated