Tallest building between parking lots
I was asked this
No reports yet. Sign in to say where you were asked it.
A city block is drawn as a rectangular grid of strings. Each tile is one of:
-
'P': a parking lot. Its height is fixed at 0. -
'-': a lot where a building can go. You choose its height (a non-negative integer). -
'X': unusable land. Nothing is built there and it imposes no rule.
Zoning rule: any two usable tiles ('P' or '-') that share an edge (up, down, left, right; not diagonal) may differ in height by at most 1.
Implement:
def max_building_height(grid: list[str]) -> int: ...
Return the height of the tallest building that can legally be placed anywhere on the grid, assuming every building is made as tall as the rule allows. Return -1 if some building tile cannot reach any parking lot through usable tiles (its height would be unbounded). If the grid has no building tiles, return 0.
Constraints: 0 <= rows, cols <= 1000; the grid is rectangular. Aim for time linear in the number of tiles.
Example 1
P--
---
--P
Answer: 2. The tiles at (0,2), (1,1) and (2,0) are each two steps from the nearest lot.
Example 2
P-X--
--X-P
Answer: 2. The X column splits the block in two; each half has its own lot, and the farthest building on either side is two steps away.
Example 3
PX-
Answer: -1. The building on the right is walled off from every lot.
Follow-up: also return the full grid of maximum heights.
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
Sign in to submit your code; every attempt is kept here.
