491. Integer Replacement
MediumGreedyBit Manipulation
If n is even replace it with n/2; if odd, with n + 1 or n − 1. Print the minimum replacements to reach 1.
Input format
One integer n
Output format
The minimum steps.
Example 1
Input
8
Output
3
Example 2
Input
7
Output
4
Constraints
1 ≤ n ≤ 2³¹ − 1
Read input from standard input and print the answer to standard output. Your code is judged against 5 test cases, most of them hidden.
Loading the MyLoginn IDE…