Garden Relay Count
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

Best Answers
import java.util.*;
class Solution {
public int garden_relay_count(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.
