Very Simple Java Solution with Explanation


  • 0
    A

    If you notice for all numbers less than 8, the largest possible multiple you can make is dividing it in 2 and multiplying, accounting for if the number is odd.

    2 = 1 + 1
    3 = 2 + 1
    4 = 2 + 2
    5 = 2 + 3
    6 = 3 + 3
    7 = 4 + 3

    You just need to subtract 3 from n until you get to n <= 7, keeping track of this product, then return this product times (n / 2) * ((n + 1) / 2)

    class Solution {
        public int integerBreak(int n) {
            int product = 1;
            while(n > 0) {
                if(n < 8) {
                    int val = (n/2) * ((n + 1)/2);
                    return product * val;
                }
                else {
                    product *= 3;
                    n -= 3;
                }
            }
            return product;
        }
    }
    

Log in to reply
 

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