O(1) space .O(1) time complexity.


  • 1
    L

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


  • 0
    M

    can you explain why does this can be accepted


  • 0
    L

    It is easy to see that 6=2+2+2=3+3 and 2*2*2<3*3. Then you will prove that a number will be breaked into 3 as many as possible


  • 0
    M

    @lixiaobo Thank you very much


Log in to reply
 

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