827. Making a Large Island¶
Edited by Charles
Hard · Python · Graph
Problem
Read the original problem on LeetCode.
The official page contains the problem statement, examples, and constraints.
Explanation, examples & other approaches
Core idea
You are given an n x n binary grid where:
You may change at most one 0 into 1.
Original study notes follow, including intermediate snippets and earlier attempts.
Idea¶
You are given an n x n binary grid where:
You may change at most one 0 into 1.
Return the size of the largest island that can be created.
The key idea is:
First, identify every existing island and record its area.
Then, for each
0, check which neighboring islands would be connected if this0were changed into1.
This problem is an extension of LeetCode 695. Max Area of Island.
Step 1: Label Every Island¶
Instead of only marking visited land as 0, assign each island a unique ID.
For example:
After labeling:
Here:
We can store the areas in a dictionary:
We start island IDs from 2 because:
Step 2: DFS to Calculate Island Area¶
For every unvisited land cell:
run DFS.
During DFS:
- change the current cell from
1to the island ID, - recursively visit all connected land cells,
- return the total island area.
The DFS can look like:
def dfs(i, j, island_id):
if i < 0 or i >= n or j < 0 or j >= n or grid[i][j] != 1:
return 0
grid[i][j] = island_id
return (
1
+ dfs(i - 1, j, island_id)
+ dfs(i + 1, j, island_id)
+ dfs(i, j - 1, island_id)
+ dfs(i, j + 1, island_id)
)
Each island gets a unique label and its area is stored.
Step 3: Try Changing Every 0 Into 1¶
Now traverse the grid again.
For every water cell:
imagine changing it into land.
The new island area starts with:
for the converted cell itself.
Then check its four neighbors:
If a neighboring cell belongs to an island, add that island's area.
Why Do We Need a Set?¶
Suppose the grid is:
The 0 touches island 2 from both the top and the left.
If we simply add the area for every neighbor:
we would count the same island twice.
Therefore, we must collect unique island IDs first.
For example:
Then:
The new area is:
This prevents double counting.
Hand Simulation¶
Consider:
Initially:
Step 1: Label Islands¶
The top-left island gets ID 2:
Its area is:
The bottom-right island gets ID 3:
Its area is:
So:
Step 2: Try the Top-Right 0¶
Consider changing:
into 1.
Its neighbors include:
So the connected island IDs are:
New area:
Step 3: Try the Bottom-Left 0¶
The same thing happens.
It also connects islands 2 and 3.
New area:
Therefore:
Larger Example¶
Consider:
After labeling islands:
Areas:
Now consider the center 0:
It touches:
So changing it into land creates:
Therefore, the largest possible island has area:
What If the Grid Has No 0?¶
For example:
There is already one island with area:
Since we may change at most one 0, we do not have to change anything.
Therefore, the answer should be:
This is why we should also keep track of the largest existing island area.
Complexity Analysis¶
Time Complexity
The grid is traversed a constant number of times.
DFS visits each land cell once.
For every 0, we only inspect four neighbors.
Space Complexity
In the worst case, the DFS recursion stack can contain O(n²) cells.
The area dictionary also stores information for all islands.
Summary¶
1. Use DFS to find every island.
2. Give each island a unique ID.
3. Store each island's area in a dictionary.
4. Traverse every 0.
5. Pretend to change the 0 into 1.
6. Collect the unique neighboring island IDs.
7. Add their areas together plus 1.
8. Take the maximum.
The most important ideas are:
Label each island first, then merge neighboring islands virtually.
and:
Use a set when checking neighbors so the same island is not counted twice.
Common Mistakes¶
- Do not simply mark visited land as
0. We need to preserve the identity of each island so that neighboring0s know which islands they touch. - Use different IDs such as:
for different islands.
- Use a
setwhen collecting neighboring island IDs. The same island may touch a0from multiple directions. - Remember to count the converted water cell itself:
- Do not forget the case where the entire grid is already land. In that case, the answer is simply:
Study record
| Field | Value |
|---|---|
| Attempts | 1 |
| Mastery | ❚ |
| The Latest Attempt | August 19, 2026 |
Python solution¶
class Solution:
def largestIsland(self, grid: List[List[int]]) -> int:
m = len(grid)
islands = {}
res = 0
island_id = 2
def dfs(i, j, island_id):
if i < 0 or i >= m or j < 0 or j >= m or grid[i][j] != 1:
return 0
grid[i][j] = island_id
return 1 + dfs(i - 1, j, island_id) + dfs(i + 1, j, island_id) + dfs(i, j - 1, island_id) + dfs(i, j + 1, island_id)
for i in range(m):
for j in range(m):
if grid[i][j] == 1:
area = dfs(i, j, island_id)
islands[island_id] = area
res = max(res, area)
island_id += 1
directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]
for i in range(m):
for j in range(m):
if grid[i][j] == 0:
neighbors = set()
temp_area = 1
for p, q in directions:
if 0 <= i + p < m and 0 <= j + q < m and grid[i + p][j + q] > 1:
neighbors.add(grid[i + p][j + q])
for neighbor in neighbors:
temp_area += islands[neighbor]
res = max(res, temp_area)
return res