Monitoring~20 min2s · 256 MBtarget O(R × C) time, O(R × C) spaceclassic twin: lc-994bfs
A monitoring system pushes a new monitoring config through a data hall without a central controller. The hall is a grid of rack slots. A slot is empty (0), holds a server still waiting for the config (1), or holds a server that already has it (2).
Each minute, every server that has the config passes it to each waiting server directly up, right, down or left of it. Your team lead wants to know how many minutes the rollout takes, so they can plan the check that follows. A hall holds up to 400 by 400 slots.
Write rollout_minutes(racks).
racks is a list of rows of 0 (empty), 1 (waiting) and 2 (updated). Every row has the same length.Input: racks = [[2, 1, 0], [1, 1, 1], [0, 0, 1]]
Output: 4Every updated server starts in the queue at minute 0. Each waiting server gets its minute when it joins.
| Taken from the queue | Its minute | Joins the back |
|---|---|---|
| (0, 0) | 0 | (0, 1) and (1, 0) at minute 1 |
| (0, 1) | 1 | (1, 1) at minute 2 |
| (1, 0) | 1 | none: (1, 1) has already joined |
| (1, 1) | 2 | (1, 2) at minute 3 |
| (1, 2) | 3 | (2, 2) at minute 4 |
| (2, 2) | 4 | none |
The last server to join gets minute 4, and no server is still waiting, so the answer is 4.
Input: racks = [[1, 0, 2], [1, 0, 1]]
Output: -1The empty middle column cuts the two left servers off. The rollout reaches (1, 2) at minute 1 and stops there.
Input: racks = [[0, 2]]
Output: 0No server is waiting, so the rollout is done at minute 0.
1 ≤ rows, columns ≤ 400, and every row has the same lengthThe size here is the number of slots: up to 400 × 400 = 160,000. One BFS over the hall handles each slot once, which is under a million steps.
Simulating minute by minute scans the whole hall every minute. A winding row of servers can take about 80,000 minutes, so that is about 80,000 × 160,000 steps: far too slow.