BudiBadu Logo

Harbor Signal Expansion Test

Array Easy 0 views
Like14

Technicians coordinate lighthouses around a harbor to broadcast patterns before a launch. Lighthouses (0 to n-1) are linked by bidirectional cables. When a signal activates at one tower, it spreads through every connected cable to neighbors. Your task in Harbor Signal Expansion Test is to count how many online towers participate in the broadcast while avoiding all Offline Towers.

The "secret sauce" here is Graph Traversal with Node Exclusion. Use BFS or DFS to explore from the start tower, but immediately skip any path that hits a tower in the maintenance set. Offline towers neither receive the signal nor pass it along. The starting tower is always online, and duplicate or self-linking cables are treated as a single connection. The goal is to measure the reach of the broadcast without double-counting any tower. Return the final count as a single integer. This audit ensures the coastal crew can rely on the coverage during emergencies!

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

Example 1
Input
n = 5, links = [[0,1],[1,2],[2,3],[3,4]], start = 3, maintenance = []
Output
5
Explanation

All towers are online, so the signal covers every lighthouse.

Example 2
Input
n = 6, links = [[0,1],[1,2],[2,3],[3,4],[4,5]], start = 0, maintenance = [3]
Output
4
Explanation

Towers 0, 1, 2, and 5 remain online; cable through tower 3 prevents reaching tower 4.

Example 3
Input
n = 4, links = [[0,1],[1,2]], start = 2, maintenance = [0,1]
Output
1
Explanation

Maintenance disables the only connecting cables, so the broadcast stays at the starting lighthouse.

Algorithm Flow

Recommendation Algorithm Flow for Harbor Signal Expansion Test - Budibadu
Recommendation Algorithm Flow for Harbor Signal Expansion Test - Budibadu

Best Answers

java
import java.util.*;
class Solution {
    public boolean has_segment_sum(int[] signals, int k) {
        Set<Integer> seen = new HashSet<>();
        seen.add(0);
        int current = 0;
        for (int x : signals) {
            current += x;
            if (seen.contains(current - k)) return true;
            seen.add(current);
        }
        return false;
    }
}