LeetCode "483. Smallest Good Base" !!

A more programming-like solution, is to hack the problem from simple: we try each possble base value, and see which 111..11 fits target number - using binary search.

class Solution(object):
    def helper(self, num, p):
        l = 1; r = int(pow(num, 1.0/p) + 1) # find highest possible bit
        while(l < r):
            mid = l + (r - l) // 2
            sum = 0; cur = 1
            # Addup to sum by form of 1111..11
            for i in xrange(0, p + 1):
                sum += cur
                cur *= mid
            # Good?
            if sum == num:
                return mid
            elif sum > num:
                r = mid
            else:
                l = mid + 1

        return -1

    def smallestGoodBase(self, N):
        n = int(N)

        # iterate each base, from longest 1s to shortest
        for p in xrange(2, 101):
            if (1 << p) < n:
                k = self.helper(n, p)
                if k != -1:
                    return str(int(k))

        return str(n-1) # 11
        

However, as you expect, there‘s a smarter, math solution.

https://discuss.leetcode.com/topic/76368/python-solution-with-detailed-mathematical-explanation-and-derivation

时间: 2024-09-30 10:48:29

LeetCode "483. Smallest Good Base" !!的相关文章

[LeetCode] 632. Smallest Range Covering Elements from K Lists

[LeetCode]632. Smallest Range Covering Elements from K Lists 你有 k 个升序排列的整数数组.找到一个最小区间,使得 k 个列表中的每个列表至少有一个数包含在其中. 我们定义如果 b-a < d-c 或者在 b-a == d-c 时 a < c,则区间 [a,b] 比 [c,d] 小. 示例 1: 输入:[[4,10,15,24,26], [0,9,12,20], [5,18,22,30]] 输出: [20,24] 解释: 列表 1:

[LeetCode] K-th Smallest in Lexicographical Order 字典顺序的第K小数字

Given integers n and k, find the lexicographically k-th smallest integer in the range from 1 to n. Note: 1 ≤ k ≤ n ≤ 109. Example: Input: n: 13 k: 2 Output: 10 Explanation: The lexicographical order is [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9], so

[LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素

Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix. Note that it is the kth smallest element in the sorted order, not the kth distinct element. Example: matrix = [ [ 1, 5

Leetcode: K-th Smallest in Lexicographical Order

Given integers n and k, find the lexicographically k-th smallest integer in the range from 1 to n. Note: 1 ≤ k ≤ n ≤ 109. Example: Input: n: 13 k: 2 Output: 10 Explanation: The lexicographical order is [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9], so

[LeetCode] 910. Smallest Range II 最小区间之二

Given an array?A?of integers, for each integer?A[i]?we need to choose?either?x = -K?or?x = K, and add?x?to?A[i]?(only once). After this process, we have some array?B. Return the smallest possible difference between the maximum value of?B?and the mini

[LeetCode] Find Smallest Letter Greater Than Target

Given a list of sorted characters letters containing only lowercase letters, and given a target letter target, find the smallest element in the list that is larger than the given target. Letters also wrap around. For example, if the target is target

[LeetCode] Kth Smallest Number in Multiplication Table 乘法表中的第K小的数字

Nearly every one have used the Multiplication Table. But could you find out the k-th smallest number quickly from the multiplication table? Given the height m and the length n of a m * n Multiplication Table, and a positive integer k, you need to ret

LeetCode 302. Smallest Rectangle Enclosing Black Pixels

DFS 最一开始想到的就是dfs,从题目给的点开始dfs搜索. 时间复杂度为O(mn) class Solution { public: int i_min=INT_MAX, i_max=INT_MIN; int j_min=INT_MAX, j_max=INT_MIN; vector<vector<int>> dirs={{1,0},{-1,0},{0,1},{0,-1}}; int minArea(vector<vector<char>>& im

[LeetCode 988] Smallest String Starting From Leaf

Given the root of a binary tree, each node has a value from 0 to 25 representing the letters 'a' to 'z': a value of 0 represents 'a', a value of 1 represents 'b', and so on. Find the lexicographically smallest string that starts at a leaf of this tre