Music streaming app~25 min2s · 256 MBtarget O(R × C) time, O(R × C) spaceclassic twin: lc-695dfs
A lobby video wall is a grid of tiles, and each tile plays a stream at a quality tier from 0 to 9. Two tiles are in the same block when they share a side and play the same tier. Tiles that touch only at a corner are not in the same block.
The operations team re-encodes the biggest block first. They want to know its tier and its size. When two blocks are equally big, they take the one with the lower tier.
Write largest_block(tiles).
tiles[r][c] is the tier of the tile in row r and column c.[tier, size] for the block with the most tiles. Among blocks with equal size, return the one with the lowest tier.Input: tiles = [[1, 1, 2, 2], [1, 3, 3, 2], [4, 3, 3, 3]]
Output: [3, 5]The solution scans the tiles in reading order. Each time it meets a tile that is not yet in a block, it floods outwards over side neighbours of the same tier and counts the tiles.
| Start tile (row, column) | Tier | Block size | Largest so far |
|---|---|---|---|
| (0, 0) | 1 | 3 | 3 |
| (0, 2) | 2 | 3 | 3 |
| (1, 1) | 3 | 5 | 5 |
| (2, 0) | 4 | 1 | 5 |
The largest block has tier 3 and 5 tiles.
Input: tiles = [[5, 5], [7, 7]]
Output: [5, 2]Both blocks have 2 tiles, so the lower tier, 5, is returned.
Input: tiles = [[9]]
Output: [9, 1]A single tile is a block of one tile.
1 ≤ rows ≤ 300 and 1 ≤ columns ≤ 3000 ≤ tiles[r][c] ≤ 9There are 90,000 tiles. Measuring the block of every tile separately repeats the same flood fill for each of its tiles, which is far too slow. One fill per block, visiting each tile once, takes about 90,000 steps. A single block can also fill the whole wall, so a recursive search would go 90,000 calls deep: use an explicit stack.