mediumGraphsAmazonFacebook
Largest Island Surveyor
A geographer ranks landmasses by acreage from a binary satellite map and reports the largest.
Given a binary grid, return the size (in cells) of the largest 4-connected island of 1s.
Choose approach
0
0
1
seed
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
1
1
1
0
0
0
0
1
1
0
1
0
0
0
0
0
0
0
0
0
1
0
0
1
1
0
0
1
0
1
0
0
1/7
Narrationstep 1 of 7
Flooded island starting (0,2) area=1. Best = 1 (new max!).
Variables
area=1
best=1
How to think about it
- Flood-fill returns 1 + sum of neighbor floods.
- Mutate visited cells to 0 to avoid re-counting.
- Max over all starting cells = answer.
Edge cases to catch in interview
- All water -> 0.
- Single land cell -> 1.
Examples
Example 1
3{"grid":[[1,1],[1,0]]}
Try your own input
SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed