This problem asks how many garden stations can be reached by a relay signal. You start at one chosen garden, and the signal can move along the given paths, but only for a limited number of steps.
The answer is a count of reachable gardens, and the starting garden counts too. If the start has no useful path, then the answer can still be 1 because the signal begins there. If the step limit is small, the signal may stop early even when the full network is much larger.
For example, if you start at garden 3 and there are no usable links connected to it, only garden 3 gets counted. In another example, starting at garden 2 with a limit of 1 step means you can only reach the gardens that are exactly one hop away, plus the starting point itself.
The key is counting only the places that can be reached before the step limit runs out. You are not counting the whole connected group, only the part that the relay can reach in time.
Example Input & Output
Garden 3 has no usable link, so only the starting location hears the relay.
Starting at garden 2 lets the caretakers reach gardens 1 and 3 within one hop.
The relay covers gardens 0, 1, and 2 before the hop limit stops the signal.
Algorithm Flow
Solution Approach
This problem asks us to count the gardens reachable from a start, but only within a limited number of steps. Unlike a plain reachability check, we must stop exploring once the step budget is used up, so we track the distance of each node during the search.
A breadth-first search is ideal here because it visits nodes in order of increasing distance. We store each node together with its distance from the start, and only expand a node further if we still have steps to spare.
Here is the implementation:
We build an undirected graph, skipping any path that references a non-existent node. We seed the queue with the start at distance 0. In the loop, we only expand a node when its current distance d is less than max_steps, pushing unvisited neighbors with distance d + 1.
Because BFS explores level by level, every node is first discovered at its shortest distance from the start. The condition d < max_steps guarantees we never travel more than the allowed number of hops, and the start itself is always counted.
The time complexity is O(n + e) and the space complexity is O(n).
Best Answers
import java.util.*;
class Solution {
public int count_relay_reach(int n, int[][] paths, int start, int max_steps) {
if (n <= 0) return 0;
List<List<Integer>> adj = new ArrayList<>();
for (int i = 0; i < n; i++) adj.add(new ArrayList<>());
for (int[] p : paths) {
if (p.length < 2) continue;
int u = p[0], v = p[1];
if (u < n && v < n) {
adj.get(u).add(v);
adj.get(v).add(u);
}
}
boolean[] visited = new boolean[n];
Queue<int[]> q = new LinkedList<>();
q.add(new int[]{start, 0});
visited[start] = true;
int count = 1;
while (!q.isEmpty()) {
int[] curr = q.poll();
int u = curr[0], d = curr[1];
if (d < max_steps) {
for (int v : adj.get(u)) {
if (!visited[v]) {
visited[v] = true;
q.add(new int[]{v, d + 1});
count++;
}
}
}
}
return count;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
