Given a positive integer n, you can apply one of the following operations:
n is even, replace n with n / 2.n is odd, replace n with either n + 1 or n - 1.Return the minimum number of operations needed for n to become 1.
Example 1:
Input: n = 8 Output: 3 Explanation: 8 -> 4 -> 2 -> 1
Example 2:
Input: n = 7 Output: 4 Explanation: 7 -> 8 -> 4 -> 2 -> 1 or 7 -> 6 -> 3 -> 2 -> 1
Example 3:
Input: n = 4 Output: 2
Constraints:
1 <= n <= 231 - 1When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To find the fewest steps to transform a number to 1, the brute force approach explores every possible path. We repeatedly apply either the rule of dividing by 2 (if the number is even) or adding or subtracting 1 (if the number is odd), until we reach 1.
Here's how the algorithm would work step-by-step:
def integer_replacement_brute_force(number): # Use a helper function for recursion.
def find_minimum_steps(current_number):
if current_number == 1:
return 0
# If the number is even, divide by 2.
if current_number % 2 == 0:
return 1 + find_minimum_steps(current_number // 2)
# Explore both addition and subtraction.
else:
add_one = 1 + find_minimum_steps(current_number + 1)
subtract_one = 1 + find_minimum_steps(current_number - 1)
# Need to choose the path with fewer steps
return min(add_one, subtract_one)
return find_minimum_steps(number)The best way to solve this is to think of it like a game where you're trying to reach one in the fewest moves. The key is to make decisions based on whether the number is even or odd, focusing on getting to a power of two efficiently.
Here's how the algorithm would work step-by-step:
def integer_replacement(number): steps_taken = 0
while number != 1:
if number % 2 == 0:
number //= 2
else:
# Handle the odd number case
if number == 3:
number -= 1
elif (number + 1) % 4 == 0:
# Subtracting one leads to more trailing zeros
number += 1
else:
# Adding one leads to more trailing zeros
number -= 1
steps_taken += 1
return steps_taken| Case | How to Handle |
|---|---|
| Input n is 1 | Return 0 immediately as no operations are needed. |
| Input n is 0 | This input is invalid according to problem definition, throw an IllegalArgumentException. |
| Input n is Integer.MAX_VALUE | Integer.MAX_VALUE + 1 would overflow causing incorrect even/odd check; special handling needed (n = n -1, ops++) or convert to long before processing. |
| Input n is a large even number close to Integer.MAX_VALUE | Divide by 2 will not cause overflow, but n+1 may when trying to optimize by adding; consider decrementing instead. |
| Recursion depth exceeding stack limit (for recursive solutions) | Use iterative approach or memoization to avoid stack overflow errors. |
| Negative input n | Input is defined as a positive integer; throw IllegalArgumentException. |
| Input n is a power of 2 | Bit manipulation can be used to efficiently solve the problem when n is a power of 2. |
| Values oscillating between adding and subtracting | The algorithm should choose the optimal operation (adding or subtracting 1) based on whether n+1 or n-1 has more trailing zeros after division by 2 to ensure the fewest steps. |