Garden Relay Count
During the solar festival, runners pass flower batons along a linear garden trail. In Garden Relay Count, your goal is to determine the length of the longest contiguous segment where the baton IDs alternate between even and odd. This balance represents a rhythmic flow that Curators use to time the festival’s choreography. Any break in this alternating rhythm ends the current segment immediately!
The "secret sauce" here is a Linear Streak Tracker. You walk through the array and check the parity of the current ID against the one before it. If they differ, your current streak grows. If they match, the rhythm is broken, and you must "reset" your streak count back to 1. By keeping track of the highest count found during your scan, you can find the peak duration of the garden’s balanced flow in O(n) time. This ensures you can process thousands of runners efficiently before the parade starts!
When the task involves connectivity or route cost, build adjacency carefully and guard against revisiting stale states. Use a visited or best-distance structure to avoid repeated work, and ensure unreachable scenarios return the required fallback value instead of partial traversal results.
Examples
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 problem.
