Practice › Graphs
Max Area of an Island
Medium · Graphs · Asked at Amazon, Google, Meta, Microsoft
In a grid of 0s (water) and 1s (land), an island is a group of 1s connected up, down, left or right. Print the area (number of cells) of the largest island, or 0 if there is no land.
Input
First line: r c. Next r lines: c values (0 or 1).
Output
The largest island area.
Constraints
- 1 ≤ r, c ≤ 300
Flood fill (DFS or BFS) from every unvisited land cell, counting the cells reached. Mark cells as visited so each is counted once.
Example 1
Input
4 5 0 0 1 0 0 1 1 1 0 0 0 0 0 1 1 0 1 0 1 1
Output
4
Example 2
Input
1 3 0 0 0
Output
0
Hints
- Every land cell belongs to exactly one island.
- Start a flood fill at each land cell not seen yet and count the cells it reaches.
- Mark cells as you visit them so nothing is counted twice.
Approach
Scan the grid. When a cell is 1, run an iterative DFS (an explicit stack avoids deep recursion on a 300 × 300 island), turning visited cells into 2 and counting them. The largest count is the answer; 0 if the grid has no land.
Complexity: O(r · c) time · O(r · c) space for the stack in the worst case
Solution
C++ solution
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int R, C;
cin >> R >> C;
vector<vector<int>> g(R, vector<int>(C));
for (auto &row : g)
for (auto &x : row) cin >> x;
int best = 0;
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
for (int i = 0; i < R; i++)
for (int j = 0; j < C; j++) {
if (g[i][j] != 1) continue;
int area = 0;
vector<pair<int, int>> st = {{i, j}};
g[i][j] = 2;
while (!st.empty()) {
auto _b = st.back();
auto x = _b.first;
auto y = _b.second;
st.pop_back();
area++;
for (int d = 0; d < 4; d++) {
int a = x + dx[d], b = y + dy[d];
if (a >= 0 && b >= 0 && a < R && b < C && g[a][b] == 1) {
g[a][b] = 2;
st.push_back({a, b});
}
}
}
best = max(best, area);
}
cout << best << "\n";
return 0;
}Python solution
import sys
data = sys.stdin.buffer.read().split()
R, C = int(data[0]), int(data[1])
g = [list(map(int, data[2 + i * C : 2 + (i + 1) * C])) for i in range(R)]
best = 0
for i in range(R):
for j in range(C):
if g[i][j] != 1:
continue
area = 0
stack = [(i, j)]
g[i][j] = 2
while stack:
x, y = stack.pop()
area += 1
for a, b in ((x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)):
if 0 <= a < R and 0 <= b < C and g[a][b] == 1:
g[a][b] = 2
stack.append((a, b))
best = max(best, area)
print(best)Java solution
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
in.nextToken();
int R = (int) in.nval;
in.nextToken();
int C = (int) in.nval;
int[][] g = new int[R][C];
for (int i = 0; i < R; i++)
for (int j = 0; j < C; j++) {
in.nextToken();
g[i][j] = (int) in.nval;
}
int[] dx = {1, -1, 0, 0}, dy = {0, 0, 1, -1};
int best = 0;
ArrayDeque<int[]> st = new ArrayDeque<>();
for (int i = 0; i < R; i++)
for (int j = 0; j < C; j++) {
if (g[i][j] != 1) continue;
int area = 0;
st.push(new int[] {i, j});
g[i][j] = 2;
while (!st.isEmpty()) {
int[] c = st.pop();
area++;
for (int d = 0; d < 4; d++) {
int a = c[0] + dx[d], b = c[1] + dy[d];
if (a >= 0 && b >= 0 && a < R && b < C && g[a][b] == 1) {
g[a][b] = 2;
st.push(new int[] {a, b});
}
}
}
best = Math.max(best, area);
}
System.out.println(best);
}
}