Simple Java 0ms solution


  • 0

    We only need to split number into 3s, and fill it up with 2s if n % 3 != 0. It's because any number greater than 3 can be split to produce a greater or equal product.

    public int integerBreak(int n) {
        if (n == 2) {
            return 1;
        }
        if (n == 3) {
            return 2;
        }
        int cnt2 = (3 - n % 3) % 3;
        int cnt3 = (n - cnt2 * 2) / 3;
        return (int) Math.pow(3, cnt3) * (int) Math.pow(2, cnt2);
    }

Log in to reply
 

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