Accepted Python Code


  • 0

    Instead of using binary search or linear search, I assume the answer of equation (1 + x + x^2 + ... + x^m) - n = 0 to be floor(pow(n, 1.0/m)). Is that correct? Can anybody prove the correctness or challenge the code below?

    class Solution(object):
        def smallestGoodBase(self, n):
            """
            :type n: str
            :rtype: str
            """
            n = int(n)
            for m in range(int(math.log(n, 2)), 1, -1):
                k = int(n ** (1.0 / m))
                if sum(k ** i for i in range(m + 1)) == n:
                    return str(k)
            return str(n - 1)
    

    Ref: http://bookshadow.com/weblog/2017/01/22/leetcode-smallest-good-base/


Log in to reply
 

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