For an integer n, we call k>=2 a good base of n, if all digits of n base k are 1.

Now given a string representing n, you should return the smallest good base of n in string format.

Example 1:

Input: "13"
Output: "3"
Explanation: 13 base 3 is 111.

Example 2:

Input: "4681"
Output: "8"
Explanation: 4681 base 8 is 11111.

Example 3:

Input: "1000000000000000000"
Output: "999999999999999999"
Explanation: 1000000000000000000 base 999999999999999999 is 11.

Note:

  1. The range of n is [3, 10^18].
  2. The string representing n is always valid and will not have leading zeros.

这道题让我们求最小的好基数,定义了一个大于等于2的基数k,如果可以把数字n转化为各位都是1的数,那么就称这个基数k是好基数。通过看题目中的三个例子,应该大致可以理解题意了吧。如果我们用k表示基数,m表示转为全1数字的位数,那么数字n就可以拆分为:

n = 1 + k + k^2 + k^3 + ... + k^(m-1)

这是一个等比数列,中学数学的内容吧,利用求和公式可以表示为 n = (k^m - 1) / (k - 1)。我们的目标是求最小的k,那么仔细观察这个式子,在n恒定的情况,k越小则m却大,就是说上面的等式越长越好。下面我们来分析m的取值范围,题目中给了n的范围,是 [3, 10^18]。由于k至少为2,n至少为3,那么肯定至少有两项,则 m>=2。再来看m的上限该如何求?其实也不难,想要m最大,k就要最小,k最小是2,那么m最大只能为 log2(n + 1),数字n用二进制表示的时候可拆分出的项最多。但这道题要求变换后的数各位都是1,那么我们看题目中最后一个例子,可以发现,当 k=n-1 时,一定能变成 11,所以实在找不到更小的情况下就返回 n-1。

下面我们来确定k的范围,由于k至少为2,那么就可以根据下面这个不等式来求k的上限:

n = 1 + k + k^2 + k^3 + ... + k^(m-1) > k^(m-1)

解出 k < n^(1 / (m-1)),其实我们也可以可以通过 n < k^m - 1 来求出k的准确的下限,但由于是二分查找法,下限直接使用2也没啥问题。分析到这里,那么解法应该已经跃然纸上了,我们遍历所有可能的m值,然后利用二分查找法来确定k的值,对每一个k值,我们通过联合m值算出总和 sum,然后跟n来对比即可,参见代码如下:

class Solution {
public:
string smallestGoodBase(string n) {
long long num = stol(n);
for (int i = log(num + ) / log(); i >= ; --i) {
long long left = , right = pow(num, 1.0 / (i - )) + ;
while (left < right) {
long long mid = left + (right - left) / , sum = ;
for (int j = ; j < i; ++j) {
sum = sum * mid + ;
}
if (sum == num) return to_string(mid);
if (sum < num) left = mid + ;
else right = mid;
}
}
return to_string(num - );
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/483

参考资料:

https://leetcode.com/problems/smallest-good-base/

https://leetcode.com/problems/smallest-good-base/discuss/96591/Java-O((logn)2)-binary-search-solution

https://leetcode.com/problems/smallest-good-base/discuss/96593/Concise-C%2B%2B-Binary-Search-solution

https://leetcode.com/problems/smallest-good-base/discuss/96590/3ms-AC-C%2B%2B-long-long-int-%2B-binary-search

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 483. Smallest Good Base 最小的好基数的更多相关文章

  1. [LeetCode] Smallest Good Base 最小的好基数

    For an integer n, we call k>=2 a good base of n, if all digits of n base k are 1. Now given a str ...

  2. Leetcode 483. Smallest Good Base

    For an integer n, we call k>=2 a good base of n, if all digits of n base k are 1. Now given a str ...

  3. 483. Smallest Good Base

    For an integer n, we call k>=2 a good base of n, if all digits of n base k are 1. Now given a str ...

  4. [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 ad ...

  5. [LeetCode] 908. Smallest Range I 最小区间

    Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K, and ...

  6. [Swift]LeetCode483. 最小好进制 | Smallest Good Base

    For an integer n, we call k>=2 a good base of n, if all digits of n base k are 1. Now given a str ...

  7. Binary Search-483. Smallest Good Base

    For an integer n, we call k>=2 a good base of n, if all digits of n base k are 1. Now given a str ...

  8. [LeetCode] 727. Minimum Window Subsequence 最小窗口子序列

    Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequenceof  ...

  9. [LeetCode] 632. Smallest Range Covering Elements from K Lists 覆盖K个列表元素的最小区间

    You have k lists of sorted integers in ascending order. Find the smallest range that includes at lea ...

随机推荐

  1. powershell玩转iis网站服务器

    1 ------------安装------------------ for win7,win8,win8.1,win10控制面板--->程序和功能--->开启关闭windows功能--- ...

  2. Lambda 表达式构建初级示例(不完整)

    直接贴代码了: using System; using System.Collections.Generic; using System.Linq; using System.Linq.Express ...

  3. Dapper - 一款轻量级对象关系映射(ORM)组件,DotNet 下

    Dapper - a simple object mapper for .Net Official Github clone: https://github.com/SamSaffron/dapper ...

  4. golang两种在for循环中使用goroutine的错误形式

    1. 闭包中使用循环体中变化的量 platground链接: https://play.golang.org/p/6x6_tuQNjUO type Value struct{ val int } fu ...

  5. 《 .NET并发编程实战》扩展阅读 - 元胞自动机 - 1 - 为什么要学元胞自动机

    先发表生成URL以印在书里面.等书籍正式出版销售后会公开内容.

  6. 10道Python常见面试题

    1.MySQL索引种类 1.普通索引 2.唯一索引 3.主键索引 4.组合索引 5.全文索引 2.索引在什么情况下遵循最左前缀的规则? 最左前缀原理的一部分,索引index1:(a,b,c),只会走a ...

  7. windows下编写dll

    dll的优点 简单的说,dll有以下几个优点: 1) 节省内存.同一个软件模块,若是以源代码的形式重用,则会被编译到不同的可执行程序中,同时运行这些exe时这些模块的二进制码会被重复加载到内存中.如果 ...

  8. Vue Stomp+SocketJS 数据报错[Object object]

    开头一句mmp tmd换位置了也没个提示!!!! 坑死爹了 <template> <div> <input type="text" v-model=& ...

  9. RVZicsr指令集

    Riscv中每个硬件线程(hart)有4096个独立地址空间的状态寄存器.我们可以通过Zicsr指令读写csr寄存器.总共有6条csr读写指令,这些指令之前都在RV32I/RV64I基础指令集里面,在 ...

  10. 实战篇丨聊一聊SSRF漏洞的挖掘思路与技巧

    在刚结束的互联网安全城市巡回赛中,R师傅凭借丰富的挖洞经验,实现了8家SRC大满贯,获得了第一名的好成绩!R师傅结合自身经验并期许新手小白要多了解各种安全漏洞,并应用到实际操作中,从而丰富自己的挖洞经 ...