Harbor Signal Expansion Test
Think of a small harbor challenge where order and timing really matter. In Harbor Signal Expansion Test, you are trying to work toward the right number by following one clear idea.
Here, you are mostly deciding whether a rule stays true while you look through the input. Sometimes that means checking if things are connected, balanced, or allowed. Sometimes it means noticing the first place where the rule breaks. The answer depends on being careful from beginning to end.
For example, if the input is n = 5, links = [[0,1],[1,2],[2,3],[3,4]], start = 3, maintenance = [], the answer is 5. All towers are online, so the signal covers every lighthouse. Another example is n = 6, links = [[0,1],[1,2],[2,3],[3,4],[4,5]], start = 0, maintenance = [3], which gives 4. Towers 0, 1, 2, and 5 remain online; cable through tower 3 prevents reaching tower 4.
This is a friendly practice problem, but it still rewards careful reading. The key is noticing the exact moment when the rule stays true or breaks.
Example Input & Output
All towers are online, so the signal covers every lighthouse.
Signal reaches towers 0, 1, and 2 from the start. Tower 3 is offline, which blocks the path to 4 and 5.
Maintenance disables the only connecting cables, so the broadcast stays at the starting lighthouse.
Algorithm Flow
Solution Approach
This problem asks us to count how many towers the signal can reach from a starting tower while skipping towers under maintenance. The links form an undirected graph, so the answer is the size of the reachable connected component of the start, excluding any maintained towers.
A breadth-first search is the natural approach. We explore outward from the start and simply refuse to visit any tower that is under maintenance, exactly like avoiding a blocked node.
Here is the implementation:
First we store the maintained towers in a set and return 0 immediately if the start itself is under maintenance. Then we build an undirected adjacency list from the links, so each link connects both directions.
We seed BFS with the start and expand through neighbors. The key condition is !blocked.has(v): we only visit a neighbor if it is neither already seen nor under maintenance. This prevents the signal from crossing through a maintained tower.
Let us trace n = 6, links = [[0,1],[1,2],[2,3],[3,4],[4,5]], start = 0, maintenance = [3]. From tower 0 we reach 1 and 2. Tower 3 is blocked, so we cannot reach 4 or 5. The visited set is {0, 1, 2}, giving a count of 3.
When there is no maintenance, the signal reaches the whole connected component of the start — for example, all 5 towers in the path from 3. And when the start is isolated with no links, only the start is counted, giving 1.
The time complexity is O(n + e) where e is the number of links, and the space complexity is O(n).
Best Answers
class Solution {
public int has_segment_sum(int n, int[][] links, int start, int[] closed) {
boolean[] blocked = new boolean[n];
for (int x : closed) blocked[x] = true;
if (blocked[start]) return 0;
java.util.List<Integer>[] g = new java.util.ArrayList[n];
for (int i = 0; i < n; i++) g[i] = new java.util.ArrayList<>();
for (int[] e : links) { g[e[0]].add(e[1]); g[e[1]].add(e[0]); }
boolean[] seen = new boolean[n];
java.util.Stack<Integer> st = new java.util.Stack<>();
seen[start] = true; st.push(start);
while (!st.isEmpty()) {
int u = st.pop();
for (int w : g[u]) if (!seen[w] && !blocked[w]) { seen[w] = true; st.push(w); }
}
int cnt = 0; for (boolean b : seen) if (b) cnt++;
return cnt;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
