Online store~20 min2s · 256 MBtarget O(m * n) time, O(m + n) spaceclassic twin: lc-73traversal
A warehouse keeps its stock counts in a grid. Each row is an aisle, each column is a bay, and each cell is the count of one shelf. A count of 0 means the scanner on that shelf reported a fault.
A faulty shelf locks its whole aisle and its whole bay. Every shelf in that aisle and in that bay must then read 0 in the locked report, even when its own scanner works.
The report is built from the original counts only. A count that became 0 because of a lock does not lock anything again. Grids reach 400 aisles by 400 bays.
Write lock_aisles_and_bays(counts).
counts[r][c] is the count of the shelf in aisle r and bay c. All rows have the same length.Input: counts = [[4, 2, 5], [3, 0, 6], [7, 8, 9]]
Output: [[4, 0, 5], [0, 0, 0], [7, 0, 9]]The only 0 is at aisle 1, bay 1. Reading the original grid gives these flags.
| Line | Holds a 0 |
|---|---|
| Aisle 0 | no |
| Aisle 1 | yes |
| Aisle 2 | no |
| Bay 0 | no |
| Bay 1 | yes |
| Bay 2 | no |
Aisle 1 and bay 1 are locked, so the cross through the middle becomes 0 and the four corners keep their counts.
Input: counts = [[1, 2], [3, 4]]
Output: [[1, 2], [3, 4]]No shelf is faulty, so the grid is unchanged.
Input: counts = [[0, 5, 6, 0], [2, 3, 4, 5], [6, 7, 8, 9]]
Output: [[0, 0, 0, 0], [0, 3, 4, 0], [0, 7, 8, 0]]The zeros are in bays 0 and 3, and both are in aisle 0. Aisle 0, bay 0 and bay 3 are locked. Bays 1 and 2 keep the counts of aisles 1 and 2.
1 ≤ counts.length ≤ 400 (aisles)1 ≤ counts[r].length ≤ 400 (bays, the same for every row)0 ≤ counts[r][c] ≤ 1,000,000 (stock count of one shelf)There are 160,000 shelves. Scanning a whole aisle and a whole bay for each shelf costs about 128,000,000 steps.
Two flag lists mark the aisles and the bays once, in 160,000 steps. A second pass then writes the answer in another 160,000 steps.