All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0301_0400.s0343_integer_break.Solution Maven / Gradle / Ivy

There is a newer version: 1.37
Show newest version
package g0301_0400.s0343_integer_break;

// #Medium #Dynamic_Programming #Math

public class Solution {
    private int[] arr;

    public int integerBreak(int n) {
        arr = new int[n + 1];
        arr[2] = 1;
        // only case involve with 1 other than 2 is 3
        return n == 3 ? 2 : dp(n);
    }

    private int dp(int n) {
        if (n <= 2) {
            return arr[2];
        } else if (arr[n] != 0) {
            return arr[n];
        } else {
            int prod = 1;
            for (int i = 2; i <= n; i++) {
                prod = Math.max(prod, i * dp(n - i));
            }
            arr[n] = prod;
        }
        return arr[n];
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy