Museum Corridor Sweep
You can think of this as a small game with a very specific goal. In Museum Corridor Sweep, you are trying to work toward the right number by following one clear idea.
This problem hides a best streak, chain, or longest piece inside a larger list. You are not always taking everything. Instead, you are searching for the strongest run that still follows the rule. A choice that looks good right now may not always lead to the best final result, so careful thinking matters.
For example, if the input is n = 6, corridors = [[0,1],[1,2],[0,2],[3,4]], start = 1, the answer is 3. Rooms 0, 1, and 2 form one connected section; rooms 3 and 4 are separate. Another example is n = 4, corridors = [[0,1],[2,3]], start = 2, which gives 2. The sweep reaches rooms 2 and 3; the other rooms remain unchecked.
This is a friendly practice problem, but it still rewards careful reading. The key is to look for the best hidden streak, not just the first one that seems okay.
Example Input & Output
Rooms 0, 1, and 2 form one connected section; rooms 3 and 4 are separate.
The sweep reaches rooms 2 and 3; the other rooms remain unchecked.
The sweep moves through every room in sequence without revisiting any room.
Algorithm Flow
Solution Approach
This problem asks us to find the size of the connected component that contains the starting room. A corridor connects two rooms, and any room reachable by following corridors from the start belongs to the same sweep. The cleanest way to count these rooms is a breadth-first or depth-first search.
BFS works well here because we simply explore outward from the starting room along every corridor, marking each room we reach. Since corridors are undirected, we treat each connection as two directed edges.
Here is the implementation:
First we build an undirected adjacency list. Each corridor [u, v] adds v to u's neighbors and u to v's neighbors, and we initialize a list for every room so isolated rooms are still represented.
We seed the BFS with the starting room and repeatedly explore its neighbors. Every unvisited neighbor is marked and added to the queue. When the queue empties, the visited set contains exactly the rooms in the start's connected component, so its size is our answer.
Let us trace n = 6, corridors = [[0,1],[1,2],[0,2],[3,4]], start = 1. From room 1, we reach 0 and 2 (and 0 connects back to 2, already visited). Rooms 3 and 4 form a separate component and are never reached. The visited set is {1, 0, 2}, giving a count of 3.
When there are no corridors, the sweep reaches only the starting room itself, so the answer is 1 — matching the example with corridors = [].
The time complexity is O(n + e) where e is the number of corridors, and the space complexity is O(n) for the graph and visited set.
Best Answers
import java.util.*;
class Solution {
public int calculate_min_workers(int n, int[][] corridors, int start) {
List<List<Integer>> g = new ArrayList<>();
for (int i = 0; i < n; i++) g.add(new ArrayList<>());
for (int[] e : corridors) {
g.get(e[0]).add(e[1]);
g.get(e[1]).add(e[0]);
}
boolean[] seen = new boolean[n];
Stack<Integer> st = new Stack<>();
seen[start] = true; st.push(start);
while (!st.isEmpty()) {
int u = st.pop();
for (int w : g.get(u)) if (!seen[w]) { seen[w] = true; st.push(w); }
}
int cnt = 0; for (boolean b : seen) if (b) cnt++;
return cnt;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
