← 返回 perplexity 的题目列表Depth-First Search Problem
类型:online_judge
Given a positive integer n, transform it to 1. The transformation process: if n is even, divide by 2; if it is odd, you can choose n - 1 or n + 1. Find the minimum number of steps to transform.
Input
An integer n, 1 ≤ n ≤ 10^9
Output
An integer representing the minimum steps to transform n to 1.
Example
Input: 8 Output: 3 Explanation: 8 -> 4 -> 2 -> 1
Input: 7 Output: 4 Explanation: 7 -> 8 -> 4 -> 2 -> 1
Example
Input
8