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] Smallest Good Base 最小的好基数的更多相关文章

  1. [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 ...

  2. [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 ...

  3. 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 ...

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

  5. 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 ...

  6. [LeetCode] Smallest Rectangle Enclosing Black Pixels 包含黑像素的最小矩阵

    An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black ...

  7. [LeetCode] Smallest Range 最小的范围

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

  8. [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 ...

  9. [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 ...

随机推荐

  1. maven库

    1.本地仓库 本地仓库是你本地的一个山寨版,只有你看的到,主要起缓存作用. 当你向仓库请求插件或依赖的时候,会先检查本地仓库里是否有.如果有则直接返回,否则会向远程仓库请求,并做缓存. 本地仓库默认在 ...

  2. CSS服务器字体

    1,首先要下载ttf文件 推荐下载网站:  https://www.dafont.com/ 2,写css样式 3,服务器字体 font-family:自己随便取个名字就行 注意url里的ttf文件和f ...

  3. 201621123060《JAVA程序设计》第九周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 2. 书面作业 本次作业题集集合 1. List中指定元素的删除(题集题目) 1.1 实验总结.并回答:列举至 ...

  4. WebSocket 聊天室加自制服务器

    自动监听本地ip 占用端口9930 打开服务器 再打开页面 输入服务器监听的ip和端口 局域网可以输入内网ip 外网连接 要输入服务器的外网ip 路由器需做好映射 实现WebSocket通信功能  和 ...

  5. SQL函数返回表的示例-Z

    create function [dbo].[GetOperateCustGroup] ( ), ) ) returns @TempTable table (MaxPrice float,MinPri ...

  6. 20145237 《Java程序设计》第10周学习总结

    20145237 <Java程序设计>第10周学习总结 教材学习内容总结 Java的网络编程 •网络编程是指编写运行在多个设备(计算机)的程序,这些设备都通过网络连接起来. •java.n ...

  7. 标准C++类std::string的内存共享和Copy-On-Write(写时拷贝)

    标准C++类std::string的内存共享,值得体会: 详见大牛:https://www.douban.com/group/topic/19621165/ 顾名思义,内存共享,就是两个乃至更多的对象 ...

  8. Android 4.4 沉浸式透明状态栏

    原文链接:http://www.bkjia.com/Androidjc/913061.html 第一种方法 这里写代码片第一种方法,在代码设置: if(VERSION.SDK_INT >= VE ...

  9. :after/:before使用技巧

    伪类:after/:before基本使用 div:before{ content:'';//必须要写,没写则伪元素无效 display:; position:''; ... } //在一个div子元素 ...

  10. MySQL ID排序乱了的解决办法

    可能在整理表中数据的时候删除了某一行数据,导致ID空缺,下面是我用到的解决办法:(请先备份,MySQL备份方法见 MySQL->MySQL备份) 使用ALTER DROP删除原有的ID字段: A ...