If you analyze, you’ll find it is better to divide NN by 2 when available.

So,

dp[n] = 1 + dp[n / 2], when N is even.

When NN is odd, we have to increase/decrease N by 1 and then divide it by 2.

dp[n] = 2 + min(dp[(n - 1)/2], dp[(n + 1)/2])

This can be solved easily with a recursive solution with the base case, if N=1N = 1, then the answer is 0.

Contributors

Statistics

86% Solution Ratio
qwerty321Earliest, Jan '20
JIANEEFastest, 0.0s
JIANEELightest, 5.5 kB
GameLordShortest, 259B
Toph uses cookies. By continuing you agree to our Cookie Policy.