Code Logo

Museum Corridor Sweep

Published atDate not available
Easy 0 views
Like0

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

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

Rooms 0, 1, and 2 form one connected section; rooms 3 and 4 are separate.

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

The sweep reaches rooms 2 and 3; the other rooms remain unchecked.

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

The sweep moves through every room in sequence without revisiting any room.

Algorithm Flow

Recommendation Algorithm Flow for Museum Corridor Sweep
Recommendation Algorithm Flow for Museum Corridor Sweep

Best Answers

java
import java.util.*;
class Solution {
    public int calculate_min_workers(int[][] workers, int length) {
        Arrays.sort(workers, (a, b) -> a[0] - b[0]);
        int res = 0, currentFar = 0, i = 0;
        while (currentFar < length) {
            int nextFar = currentFar;
            while (i < workers.length && workers[i][0] <= currentFar) {
                nextFar = Math.max(nextFar, workers[i][1]);
                i++;
            }
            if (nextFar == currentFar) return -1;
            res++;
            currentFar = nextFar;
        }
        return res;
    }
}