Java DP solution


  • 0
    A
    public class Solution {
        public int integerBreak(int n) {
            int[] dp = new int[n+1];
            for(int i = 1; i <= n; i++) {
                for(int j = 1; j <= i / 2; j++) {
                    int t1 = j * (i-j);
                    int t2 = dp[j] * dp[i-j];
                    int t3 = j * dp[i-j];
                    int t4 = dp[j] * (i-j);
                    int t = Math.max(Math.max(t1, t2), Math.max(t3, t4));
                    dp[i] = Math.max(dp[i], t);
                }
            }
            return dp[n];
        }
    }

Log in to reply
 

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