Seaport Ferry Gate Control
This challenge becomes much easier once you know exactly what to keep, change, or count. In Seaport Ferry Gate Control, you are trying to work toward the right number by following one clear idea.
Control seaport ferry gate timing A good way to think about it is to first understand what goes in, then what rule you must follow, and finally what shape the answer should have.
For example, if the input is n = 5, routes = [[0,2],[2,4],[1,2],[3,4],[0,1]], start = 4, closed_gates = [1], the answer is 3. Starting at gate 4 allows access to gates 2 and 3 even though gate 1 is closed. Another example is n = 4, routes = [[0,1],[1,2]], start = 3, closed_gates = [], which gives 1. Gate 3 has no connecting routes, so only the origin counts toward the total.
This is a friendly practice problem, but it still rewards careful reading. The key is understanding the rule clearly and then applying it carefully.
Example Input & Output
Starting at gate 4 allows access to gates 2 and 3 even though gate 1 is closed.
Gate 3 has no connecting routes, so only the origin counts toward the total.
Gates 1, 0, and 2 stay connected, while gates beyond the closed gate 3 are unreachable.
Algorithm Flow

Best Answers
import java.util.*;
class Solution {
public int ferry_gate_control(int n, int[][] routes, int start, int[] closed_gates) {
Set<Integer> closed = new HashSet<>();
for (int g : closed_gates) closed.add(g);
if (closed.contains(start)) return 0;
List<List<Integer>> adj = new ArrayList<>();
for (int i = 0; i < n; i++) adj.add(new ArrayList<>());
for (int[] route : routes) {
int u = route[0], v = route[1];
if (!closed.contains(u) && !closed.contains(v)) {
adj.get(u).add(v);
adj.get(v).add(u);
}
}
Set<Integer> visited = new HashSet<>();
visited.add(start);
Queue<Integer> queue = new LinkedList<>();
queue.add(start);
while (!queue.isEmpty()) {
int curr = queue.poll();
for (int neighbor : adj.get(curr)) {
if (!visited.contains(neighbor)) {
visited.add(neighbor);
queue.add(neighbor);
}
}
}
return visited.size();
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
