Simple Java Non-DP Solution


  • 0
    D

    5-> 2 3 = 6
    6-> 3 3 = 9
    7-> 4 3 = 6
    8-> 3 3 2 = 18

    In order to produce maximum product, every number (greater than 4)should be broken down into multiple 3s.

    public class Solution {
        public int integerBreak(int n) {
            if (n==2 || n==3) {return n-1;}
            int res = 1;
            while (n > 4) {
                res *= 3;
                n -= 3;
            }
            return res*n;
        }
    }

Log in to reply
 

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