Icon for Bryon Tjanaka

Integer Replacement

LeetCode problem 397.

January 13, 2020

This is a problem I brought along to the WICSxACM Technical Interview Prep workshop on January 13, 2020.

Problem Description

Source: LeetCode 397

Given a positive integer nn, you can perform the following operations:

  1. If nn is even, replace nn with n/2n/2
  2. If nn is odd, replace nn with either n+1n + 1 or n1n - 1

What is the minimum number of replacements needed for nn to become 11?

Hints

  1. The initial temptation is to go for a fancy, math-based O(1)O(1) solution, but there are more general approaches that will increase your chances of actually coming up with a solution. What are some other approaches you can think of?
  2. How can you think of the problem as a graph? (For this workshop, I know there have been quite a few other graph problems, so this will be fresh in your mind.)

Solution

I have implemented one possible solution to the problem in C++.

Approach 1: BFS

This solution was suggested in the Hints. The idea is to think of the numbers as a graph, where each number is a node and there are edges connecting each node xx to x/2x/2 if xx is even, or to x+1x + 1 and x1x - 1 if xx is odd. We can then perform a BFS (Breadth-First Search) on the graph to find the length of the shortest path from nn to 11.

  • Time complexity: O(n)O(n) – we can only encounter numbers in the range [1,2n][1, 2n] since if you reach 2n2n, you wasted a bunch of steps because 2n2n can just be divided by 22 to get nn. Since we never visit the same number twice, the time complexity is O(2n)=O(n)O(2n) = O(n)
  • Space complexity: O(n)O(n) – same reason as above.
class Solution {
public:
int integerReplacement(int n) {
// Queue for BFS -- each pair holds a number `x` and the number
// of steps it is from `n`. We use `long long` to account for the
// edge case when `n == INT_MAX`. Note that as we pop nodes from
// and add nodes to this queue, the number of steps gradually
// increases. Furthermore, note that we are guaranteed to visit all nodes
// that are `k` steps from `n` before any nodes that are `k + 1` steps
// from `n`.
queue<pair<long long, int>> q;

// Visited set -- holds numbers that have already been encountered.
// Numbers enter this set when they are first encountered by the
// algorithm. We do not want to revisit these numbers because we only
// care about the minimum number of steps to visit them.
unordered_set<long long> visited;

// The queue starts with `n` at 0 steps, and `n` is visited to begin with.
q.push({n, 0});
visited.insert(n);

// BFS loop.
while (!q.empty()) {
// Retrieve the next `x` and `steps` from the queue.
long long x;
int steps;
tie(x, steps) = q.front(); // See std::tie
q.pop();

// We solved the problem -- return the number of steps.
if (x == 1) return steps;

if (x % 2 == 0) {
// If x is even, the only node connected to it will be `x/2`
if (visited.find(x / 2) == visited.end()) {
// Make sure to record that `x/2` is visited.
visited.insert(x / 2);
// Add `x/2` with one more step than `x`.
q.push({x / 2, steps + 1});
}
} else {
// Same as above, except we now have `x + 1` and `x - 1`
if (visited.find(x + 1) == visited.end()) {
visited.insert(x + 1);
q.push({x + 1, steps + 1});
}
if (visited.find(x - 1) == visited.end()) {
visited.insert(x - 1);
q.push({x - 1, steps + 1});
}
}
}

// If for some reason no solution was found.
return -1;
}
};

Alternate Approaches

If you look at the discussions on the original LeetCode problem, you will see references to dynamic programming (DP) and recursion.