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
Solution Approach
This problem asks us to count how many gates the ferry can reach from a starting gate, skipping any closed gates. The routes form an undirected graph, and we must build the graph without any edge that touches a closed gate, then count the connected component of the start.
The key detail is handling the case where the start itself is closed: in that situation no travel is possible, so the answer is 0. Otherwise we build the graph with only open gates and run a standard BFS.
Here is the implementation:
We put the closed gates in a set and immediately return 0 if the start is closed. While building the graph, we only add an edge when neither endpoint is closed, which removes closed gates and their routes. Then BFS from the start counts the reachable component.
Let us trace n = 5, routes = [[0,2],[2,4],[1,2],[3,4],[0,1]], start = 4, closed_gates = [1]. Gate 1 is closed, so routes touching it are dropped, leaving edges [0,2], [2,4], and [3,4]. From gate 4 we reach 2, 0, and 3, giving 3. When the start is isolated with no routes, only the origin is counted, giving 1.
The time complexity is O(n + e) and the space complexity is O(n).
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.
