This question evaluates grid traversal and connected-component identification skills, specifically handling ragged 2D arrays, 4-directional adjacency, axis-aligned bounding-box computation, and index-to-label coordinate encoding.
You are given a 2D grid representing a map, where each cell is either 1 (part of a house) or * (empty). A house is a maximal group of 1 cells connected 4-directionally (up, down, left, right).
Unlike a standard matrix, the grid is ragged: each row may have a different number of columns.
Return the bounding rectangle of every house as a pair:
Coordinates must be formatted like spreadsheet cells:
A
for row 0,
B
for row 1, …
1
, column 1 →
2
, …
So row 0, col 1 is A2.
{ '1', '*' }
.
[topLeft, bottomRight]
.
Grid (ragged):
[* * * * 1 1 1 * *]
[* 1 1 * *]
[* * 1 1 * * *]
Interpretation: a cell exists only where a row has that column index.
Z
(e.g.,
AA
,
AB
, ...) or state that inputs are limited to 26 rows.
<= 2e5
).
Design an algorithm to find all houses and their bounding boxes.