Opening the reading…
Opening the reading…
GRAPHS › DFS AND BFS ON GRAPHS
Open — the attempt gate is not wired up yet
This editorial is meant to unlock after you have run the problem at least once, with the worked solution behind one further deliberate click. That needs per-learner unlock state nothing stores today, so for now the whole article is open.
Try it yourself first →Treat every nonnegative integer as a state. From a state cur, one operation can move to cur - 1, cur + 1, cur / 5 when divisible by 5, or cur / 11 when divisible by 11. Every move costs exactly one operation, so the answer is the length of the shortest path from x to y in this unweighted graph.
When x is already below y, increasing directly is optimal: every useful path must gain at least y - x, and adding a division would only move away from the target before requiring more increases. When x is above y, repeated decrements give a fallback of x - y operations. A better path may first increase to a nearby multiple of 5 or 11, divide, and then finish with fewer moves.
BFS explores states in order of the number of operations used to reach them. The first time it reaches y, no later path can be shorter. We only need to explore up to x + (x - y): the direct-decrement path already costs x - y, so any path that climbs above this limit has already spent more operations increasing than that complete candidate costs.
| MEASURE | BOUND | WHY |
|---|---|---|
| Time | O(U) | Each integer from 0 through U enters the queue at most once. Each state checks at most four constant-time transitions, so the total work is proportional to the number of explored states rather than to the number of possible paths. |
| Space | O(U) extra | The distance array and queue together store at most one record per bounded state; the graph is generated on demand, so no edge list is stored. The returned value is a single integer and is output storage, not working memory. In the worst shape of the input range, x = 10^4 and y = 1, U is 19999. |
#include <queue>
#include <vector>
using namespace std;
class Solution {
public:
int minimumOperationsToMakeEqual(int x, int y) {
if (x == y) {
return 0;
}
if (x < y) {
return y - x;
}
int limit = x + (x - y);
vector<int> distance(limit + 1, -1);
queue<int> states;
distance[x] = 0;
states.push(x);
while (!states.empty()) {
int cur = states.front();
states.pop();
int steps = distance[cur];
if (cur == y) {
return steps;
}
if (cur > 0 && distance[cur - 1] == -1) {
distance[cur - 1] = steps + 1;
states.push(cur - 1);
}
if (cur < limit && distance[cur + 1] == -1) {
distance[cur + 1] = steps + 1;
states.push(cur + 1);
}
if (cur % 5 == 0 && distance[cur / 5] == -1) {
distance[cur / 5] = steps + 1;
states.push(cur / 5);
}
if (cur % 11 == 0 && distance[cur / 11] == -1) {
distance[cur / 11] = steps + 1;
states.push(cur / 11);
}
}
return -1;
}
};The limit is derived from the fallback path, not chosen as an arbitrary large constant. For x > y, the direct route takes d = x - y operations. Reaching a value greater than x + d requires more than d increments from the starting value alone, so that route is already longer than a known valid answer. The optimal path therefore remains inside the bounded array.
The divisibility tests must happen before the quotient is enqueued. Division by 5 and division by 11 are edges only from exact multiples; the problem does not permit rounding. Marking a state when enqueuing it is equally important, because all edges have unit cost and the first route to a state is the shortest route to it.