Skip to content

Commit 2c4a331

Browse files
committed
Add solution #483
1 parent 1df7bf8 commit 2c4a331

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@
388388
480|[Sliding Window Median](./0480-sliding-window-median.js)|Hard|
389389
481|[Magical String](./0481-magical-string.js)|Medium|
390390
482|[License Key Formatting](./0482-license-key-formatting.js)|Easy|
391+
483|[Smallest Good Base](./0483-smallest-good-base.js)|Hard|
391392
485|[Max Consecutive Ones](./0485-max-consecutive-ones.js)|Easy|
392393
486|[Predict the Winner](./0486-predict-the-winner.js)|Medium|
393394
491|[Non-decreasing Subsequences](./0491-non-decreasing-subsequences.js)|Medium|

solutions/0483-smallest-good-base.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* 483. Smallest Good Base
3+
* https://leetcode.com/problems/smallest-good-base/
4+
* Difficulty: Hard
5+
*
6+
* Given an integer n represented as a string, return the smallest good base of n.
7+
*
8+
* We call k >= 2 a good base of n, if all digits of n base k are 1's.
9+
*/
10+
11+
/**
12+
* @param {string} n
13+
* @return {string}
14+
*/
15+
var smallestGoodBase = function(n) {
16+
const num = BigInt(n);
17+
for (let m = Math.floor(Math.log2(Number(n))); m >= 1; m--) {
18+
const k = BigInt(Math.floor(Number(n) ** (1 / m)));
19+
if (k < 2n) continue;
20+
const sum = (k ** BigInt(m + 1) - 1n) / (k - 1n);
21+
if (sum === num) return k.toString();
22+
}
23+
return (num - 1n).toString();
24+
};

0 commit comments

Comments
 (0)