Python solution with explanation

  • 0

    This problem is about break n in to x numbers, each number is about n/x, want to maximize (n/x)^x
    lets take y=(n/x)^x
    therefore log(y)=x*(log(n)-log(x))
    dlog(y)/dx=(log(n)-log(x))+x*(-1/x) set derivative to 0 to maximize
    In other works, you want to break the number to as many e's as possible.
    but since we need to break it into integer, the closest is 3.
    so break n in to as many 3's as possible.
    here is the code:

       if n==2:
         return 1
        if n==3:
            return 2
        if n==4:
            return 4
        if n==5:
            return 6
        if k==0:
            return 3**x
        if k==1:
            return 3**(x-1)*4
            return 3**x*2

Log in to reply

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