BudiBadu Logo

Museum Corridor Sweep

Array Easy 1 views
Like24

The museum’s security team monitors corridors linked by sensors (0 to n-1). During a system upgrade, certain corridor segments are Offline. Your mission in Museum Corridor Sweep is to find the Shortest Path (minimum number of sensor points) to move from a specific start area to an exit while avoiding all offline zones.

The "secret sauce" here is Breadth-First Search (BFS). Since you want the quickest route through the layout, BFS is the gold standard. It explores level by level, ensuring the first time you reach the exit, you've found the shortest possible path. Crucially, you must skip any nodes in the offline_zones list. If no path exists, return -1 correctly. This sweep ensures the guards can respond to alarms instantly, even when the facility's map is partially limited. It’s a practical application of grid or network navigation and a cornerstone of modern security logistics!

When the task involves connectivity or route cost, build adjacency carefully and guard against revisiting stale states. Use a visited or best-distance structure to avoid repeated work, and ensure unreachable scenarios return the required fallback value instead of partial traversal results.

Examples

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 - Budibadu
Recommendation Algorithm Flow for Museum Corridor Sweep - Budibadu

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;
    }
}