Code Logo

Interstate Rescue Network Audit

Published atDate not available
Medium 0 views
Like0

This challenge becomes much easier once you know exactly what to keep, change, or count. In Interstate Rescue Network Audit, 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 = 6, roads = [[0,1],[1,2],[2,3],[3,4],[4,5]], start = 0, closed_highways = [[2,3]], the answer is 3. Depots 0, 1, and 2 receive the alert; the closure prevents reaching the rest. Another example is n = 5, roads = [[0,1],[1,2],[2,3],[3,4]], start = 4, closed_highways = [], which gives 5. With no closures, the activation spreads to all depots.

This problem needs a little more patience than a very easy one. The key is noticing the exact moment when the rule stays true or breaks.

Example Input & Output

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

Depots 0, 1, and 2 receive the alert; the closure prevents reaching the rest.

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

With no closures, the activation spreads to all depots.

Example 3
Input
n = 4, roads = [[0,1],[1,2],[2,3]], start = 2, closed_highways = [[1,2],[2,3]]
Output
1
Explanation

The origin depot is surrounded by closed highways, so only itself is active.

Algorithm Flow

Recommendation Algorithm Flow for Interstate Rescue Network Audit
Recommendation Algorithm Flow for Interstate Rescue Network Audit

Best Answers

java
import java.util.*;
class Solution {
    public int rescue_network_reach(int n, int[][] roads, int start, int[][] closed_highways) {
        Set<String> closed = new HashSet<>();
        for (int[] r : closed_highways) {
            int u = Math.min(r[0], r[1]);
            int v = Math.max(r[0], r[1]);
            closed.add(u + "-" + v);
        }

        Map<Integer, List<Integer>> adj = new HashMap<>();
        for (int[] r : roads) {
            int u = Math.min(r[0], r[1]);
            int v = Math.max(r[0], r[1]);
            if (!closed.contains(u + "-" + v)) {
                adj.computeIfAbsent(r[0], k -> new ArrayList<>()).add(r[1]);
                adj.computeIfAbsent(r[1], k -> new ArrayList<>()).add(r[0]);
            }
        }

        Set<Integer> visited = new HashSet<>();
        Queue<Integer> queue = new LinkedList<>();
        
        visited.add(start);
        queue.offer(start);
        
        while (!queue.isEmpty()) {
            int u = queue.poll();
            if(adj.containsKey(u)) {
                for (int v : adj.get(u)) {
                    if (visited.add(v)) {
                        queue.offer(v);
                    }
                }
            }
        }
        
        return visited.size();
    }
}