Free written solution

Integer Replacement (LeetCode 397): Solution and Explanation

Halve n when it is even, step it up or down by one when it is odd, and return the fewest moves that bring it to 1.

Read the intuition, approach, code and complexity →