← 返回 bytedance 的题目列表Number of Islands with Large Map Follow-up
类型:online_judge
Problem: Number of Islands, with Large Map Follow-up
Given a 2D grid grid consisting of '1' and '0', where:
'1' represents land;
'0' represents water.
An island is formed by horizontally or vertically adjacent land cells. You may assume that the grid is surrounded by water.
Return the number of islands in the grid.
Follow-up
What if the map is too large to fit into memory?
Describe a scalable design, for example:
Split the map into blocks/chunks;
Count connected components inside each block;
Merge land components touching across neighboring block boundaries;
Use Union Find / Disjoint Set Union to maintain island merges across blocks.
Input Format
m n
row_0
row_1
...
row_{m-1}
Each row may be a continuous string like 11000, or space-separated values like 1 1 0 0 0.
Output Format
Print one integer, the number of islands.
Constraints
Base problem:
1 <= m, n <= 300
grid[i][j] in {'0', '1'}
Follow-up scenario:
The map is much larger than memory;
Only chunks or streams of the input can be loaded at once.
Example
Input:
4 5
11110
11010
11000
00000
Output:
1
Example
Input
4 5
11110
11010
11000
00000
Output
1