Simple Java O(1) Solution With Short Explanation


  • 0
    K

    The maximum product will be something like 3 * 3 * 3 or 3 * 3 * 4 or 3 * 3 * 2 - the power of 3 and the last multiplier depending on a remainder of n devision on 3.

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

Log in to reply
 

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