0397. Integer Replacement
https://leetcode.com/problems/integer-replacement
Description
Given a positive integer n
, you can apply one of the following operations:
If
n
is even, replacen
withn / 2
.If
n
is odd, replacen
with eithern + 1
orn - 1
.
Return the minimum number of operations needed for n
to become 1
.
Example 1:
Example 2:
Example 3:
Constraints:
1 <= n <= 231 - 1
ac
Last updated