The problem requires you to write a function that takes a positive integer n
and returns the minimum number of replacements needed for n
to become 1. The replacements can be:
n
is even, replace n
with n / 2
.n
is odd, you can replace n
with either n + 1
or n - 1
.To ensure full understanding of the problem, here are some clarifications:
n
is a positive integer.The problem can be approached using a Breadth-First Search (BFS) for optimality since BFS finds the shortest path in an unweighted graph and in this case, we can think of each number as a graph node.
(n, 0)
where 0
represents the current depth (number of steps taken).(number / 2, steps + 1)
.(number + 1, steps + 1)
and (number - 1, steps + 1)
unless those values have been visited.Here’s the implementation of the explained strategy:
from collections import deque
def integerReplacement(n: int) -> int:
# Edge case, if already n is 1
if n == 1:
return 0
queue = deque([(n, 0)])
visited = set([n])
while queue:
current, steps = queue.popleft()
if current == 1:
return steps
next_steps = steps + 1
if current % 2 == 0:
next_num = current // 2
if next_num not in visited:
visited.add(next_num)
queue.append((next_num, next_steps))
else:
next_num1 = current + 1
next_num2 = current - 1
if next_num1 not in visited:
visited.add(next_num1)
queue.append((next_num1, next_steps))
if next_num2 not in visited:
visited.add(next_num2)
queue.append((next_num2, next_steps))
The time complexity for this solution is O(log n):
The space complexity is O(log n) as well due to the nature of the BFS kept in the queue and the visited set maintaining states of up to log n due to the halving steps.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?