Monitoring~30 min2s · 256 MBtarget O(R × C) time, O(R × C) spaceclassic twin: lc-417dfs
A data-centre floor is a grid of racks, each with a heat reading. Heat moves from a rack to a side neighbour that is strictly cooler, and it keeps moving while such a neighbour exists.
Cooling aisle A runs along the first row and first column. Aisle B runs along the last row and last column. A rack's heat is removed when it reaches any rack on an aisle's edge. A rack whose heat can reach only one aisle is a single point of failure.
Write one_sided_racks(heat).
heat[r][c] is the reading of the rack in row r and column c.Input: heat = [[3, 3, 2, 4], [4, 2, 5, 1], [2, 6, 3, 3]]
Output: 6The solution searches backwards from the aisles. From an edge rack it steps to a strictly hotter neighbour, because that neighbour can send heat into it. Doing this for each aisle marks every rack that reaches it.
| Row | Heat | Aisles that the rack's heat reaches |
|---|---|---|
| 0 | [3, 3, 2, 4] | A, A, A, AB |
| 1 | [4, 2, 5, 1] | AB, none, AB, B |
| 2 | [2, 6, 3, 3] | AB, AB, B, B |
6 racks reach exactly one aisle. The others reach both.
Input: heat = [[1, 1], [1, 1]]
Output: 2All readings are equal, so heat cannot move. Each rack reaches only the aisles it stands on. The corner racks (0, 1) and (1, 0) each touch both aisles, (0, 0) touches only A and (1, 1) only B, so 2 racks reach one aisle.
Input: heat = [[5]]
Output: 0The only rack is on every edge, so it reaches both aisles.
1 ≤ rows ≤ 300 and 1 ≤ columns ≤ 3000 ≤ heat[r][c] ≤ 10^9There are 90,000 racks. Following the heat forward from each rack repeats long chains for every rack, and a ramp makes each search visit most of the floor. Two backward searches from the edges visit each rack at most once per aisle, so the work is about 180,000 steps.