Interstate Rescue Network Audit
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
Depots 0, 1, and 2 receive the alert; the closure prevents reaching the rest.
With no closures, the activation spreads to all depots.
The origin depot is surrounded by closed highways, so only itself is active.
Algorithm Flow
Solution Approach
This problem asks us to count how many depots the rescue alert can reach, excluding certain closed highways. The roads form an undirected graph, and we must build the graph while skipping any edge that is listed as closed, then count the connected component of the start.
The key detail is that a closed highway must be identified regardless of the order of its two endpoints. Since roads are undirected, we normalize each edge to a canonical key (the smaller node first) so we can check membership reliably.
Here is the implementation:
First we build a set of closed-highway keys, normalizing each pair so [2,3] and [3,2] are treated identically. Then we build the adjacency map from the open roads only, skipping any edge whose key is in the closed set. Finally we run BFS from the start and count visited nodes.
Let us trace n = 6, roads = [[0,1],[1,2],[2,3],[3,4],[4,5]], start = 0, closed_highways = [[2,3]]. The edge [2,3] is closed, so the graph splits into {0,1,2} and {3,4,5}. From depot 0, we reach 1 and 2, giving 3. With no closures, all depots are reachable and the answer is 5.
The time complexity is O(n + e) and the space complexity is O(n).
Best Answers
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();
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
