Skip to content
This repository was archived by the owner on Sep 22, 2021. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions LeetCode/0483_Smallest_Good_Base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import math

class Solution(object):
def smallestGoodBase(self, n):
"""
:type n: str
:rtype: str
"""
n = int(n)

for length in range(64, 2, -1):
l = 2
r = int(math.sqrt(n) + 1)

def sum(mid):
res = 0
for i in range(length - 1, -1, -1):
res += mid ** i
if res > n:
return res
return res

while l < r - 1:
mid = (l + r) // 2
if sum(mid) <= n:
l = mid
else:
r = mid

if sum(l) == n:
return str(l)

return str(n - 1)