Integer Replacement

Reduce a positive integer n down to 1 using a set of allowed operations. If n is even, divide it by 2. If n is odd, you may add 1 or subtract 1. Return the minimum number of operations needed to reach 1.

Examples
Input: 8
Output: 3
Hints

Integer Replacement

Reduce a positive integer `n` down to 1 using a set of allowed operations. If `n` is even, divide it by 2. If `n` is odd, you may add 1 or subtract 1. Return the minimum number of operations needed to reach 1.