Code Logo

Harbor Signal Expansion Test

Published atDate not available
Easy 0 views
Like0

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

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
Recommendation Algorithm Flow for Harbor Signal Expansion Test

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;
    }
}