BudiBadu Logo
00:00

Garden Relay Count

Graph Easy 0 views

The botanical caretakers have been wiring lanterns between the gardens of a sprawling park to coordinate nightly maintenance runs. Each garden is labelled from 0 to n - 1, and every lantern link ties two gardens together so a message can travel in either direction. When the head caretaker activates a relay at one garden, the signal hops from garden to garden along the available links. The team wants a quick headcount of how many gardens can be reached if the relay is only allowed to travel a limited number of hops.

Your task is to write a function that receives the total number of gardens n, a list of lantern links, the starting garden start, and an integer max_steps describing how many hops the relay is allowed to take. Each link is written as a pair [u, v], representing a lantern cable connecting garden u and garden v. The list may include repeated entries or self-links if the caretaker catalog captured duplicates. The order of the pairs is irrelevant, and the signal can traverse the link in either direction.

The caretaker counts every unique garden that hears the announcement within max_steps hops, including the starting garden. A hop counts each time the message crosses a link, so max_steps = 0 means the relay never leaves start. If there are links to gardens beyond the hop limit, they do not contribute to the total. Isolated gardens or unused duplicates should not inflate the result. Return the number of distinct gardens the caretaker can confirm as notified under these rules.

Example 1:

Input: n = 5, paths = [[0,1],[1,2],[2,3],[3,4]], start = 0, max_steps = 2
Output: 3
Explanation: The relay covers gardens 0, 1, and 2 before the hop limit stops the signal.

Example 2:

Input: n = 6, paths = [[0,1],[1,2],[2,3],[3,0],[4,5]], start = 2, max_steps = 1
Output: 3
Explanation: Starting at garden 2 lets the caretakers reach gardens 1 and 3 within one hop.

Example 3:

Input: n = 4, paths = [[0,1],[2,2]], start = 3, max_steps = 3
Output: 1
Explanation: Garden 3 has no usable link, so only the starting location hears the relay.

Related Problems

No related problems found

Comments (0)

Join the Discussion

Share your thoughts, ask questions, or help others with this problem.

BudiBadu Logo

Garden Relay Count

Graph Easy 0 views

The botanical caretakers have been wiring lanterns between the gardens of a sprawling park to coordinate nightly maintenance runs. Each garden is labelled from 0 to n - 1, and every lantern link ties two gardens together so a message can travel in either direction. When the head caretaker activates a relay at one garden, the signal hops from garden to garden along the available links. The team wants a quick headcount of how many gardens can be reached if the relay is only allowed to travel a limited number of hops.

Your task is to write a function that receives the total number of gardens n, a list of lantern links, the starting garden start, and an integer max_steps describing how many hops the relay is allowed to take. Each link is written as a pair [u, v], representing a lantern cable connecting garden u and garden v. The list may include repeated entries or self-links if the caretaker catalog captured duplicates. The order of the pairs is irrelevant, and the signal can traverse the link in either direction.

The caretaker counts every unique garden that hears the announcement within max_steps hops, including the starting garden. A hop counts each time the message crosses a link, so max_steps = 0 means the relay never leaves start. If there are links to gardens beyond the hop limit, they do not contribute to the total. Isolated gardens or unused duplicates should not inflate the result. Return the number of distinct gardens the caretaker can confirm as notified under these rules.

Example 1:

Input: n = 5, paths = [[0,1],[1,2],[2,3],[3,4]], start = 0, max_steps = 2
Output: 3
Explanation: The relay covers gardens 0, 1, and 2 before the hop limit stops the signal.

Example 2:

Input: n = 6, paths = [[0,1],[1,2],[2,3],[3,0],[4,5]], start = 2, max_steps = 1
Output: 3
Explanation: Starting at garden 2 lets the caretakers reach gardens 1 and 3 within one hop.

Example 3:

Input: n = 4, paths = [[0,1],[2,2]], start = 3, max_steps = 3
Output: 1
Explanation: Garden 3 has no usable link, so only the starting location hears the relay.

00:00
Loading editor...
Test Results

Run your code to see test results

Click the Submit button to execute your solution

Related Problems

No related problems found

Comments (0)

Join the Discussion

Share your thoughts, ask questions, or help others with this problem.