File tree 2 files changed +25
-0
lines changed
2 files changed +25
-0
lines changed Original file line number Diff line number Diff line change 388
388
480|[ Sliding Window Median] ( ./0480-sliding-window-median.js ) |Hard|
389
389
481|[ Magical String] ( ./0481-magical-string.js ) |Medium|
390
390
482|[ License Key Formatting] ( ./0482-license-key-formatting.js ) |Easy|
391
+ 483|[ Smallest Good Base] ( ./0483-smallest-good-base.js ) |Hard|
391
392
485|[ Max Consecutive Ones] ( ./0485-max-consecutive-ones.js ) |Easy|
392
393
486|[ Predict the Winner] ( ./0486-predict-the-winner.js ) |Medium|
393
394
491|[ Non-decreasing Subsequences] ( ./0491-non-decreasing-subsequences.js ) |Medium|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments