2dbi
Home/Apple/Number of Islands
AApple·DSASDE-2Onsite – Coding 1

Number of Islands

Problem

Given an m × n binary grid ('1' land, '0' water), count the number of islands formed by horizontally/vertically adjacent land.

Example

grid = [[1,1,0],[1,0,0],[0,0,1]]
Output: 2

Constraints

  • 1 ≤ m, n ≤ 300

Follow-up

Solve with Union-Find, and discuss how you would handle a grid that streams row by row and cannot fit in memory.

added 6 days ago
LeadersAccount