695. Max Area of Island
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
Method: Depth-First Search (DFS)
This solution finds the maximum area of an island in a grid by systematically exploring and measuring each island. The core idea is to treat the grid as a graph and use a traversal algorithm like Depth-First Search (DFS) to find connected components of land cells (1s).
Solution
Max Area of Island in Python
class Solution:
def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
if not grid or not grid[0]:
return 0
m, n = len(grid), len(grid[0])
max_area = 0
def dfs(r, c):
# Base case: if out of bounds or water/visited cell, return 0 area.
if not (0 <= r < m and 0 <= c < n and grid[r][c] == 1):
return 0
# Mark the current land cell as visited by sinking it (1 -> 0).
grid[r][c] = 0
# The area is 1 (for the current cell) + area of connected neighbors.
return (1 + dfs(r + 1, c) + # Down
dfs(r - 1, c) + # Up
dfs(r, c + 1) + # Right
dfs(r, c - 1)) # Left
# Iterate through each cell of the grid.
for i in range(m):
for j in range(n):
# If we find an unvisited land cell, start DFS to find its area.
if grid[i][j] == 1:
current_area = dfs(i, j)
max_area = max(max_area, current_area)
return max_area
Max Area of Island in Java
class Solution {
public int maxAreaOfIsland(int[][] grid) {
if (grid == null || grid.length == 0) {
return 0;
}
int m = grid.length;
int n = grid[0].length;
int maxArea = 0;
// Iterate through each cell of the grid
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
// If a land cell (1) is found, it's the start of a new island
if (grid[i][j] == 1) {
// Calculate the area of this island and update maxArea
maxArea = Math.max(maxArea, dfs(grid, i, j, m, n));
}
}
}
return maxArea;
}
// Helper function to perform Depth First Search (DFS)
private int dfs(int[][] grid, int r, int c, int m, int n) {
// Base cases for recursion termination:
// 1. Check for out-of-bounds.
// 2. Check if the cell is water (0) or already visited.
if (r < 0 || r >= m || c < 0 || c >= n || grid[r][c] == 0) {
return 0;
}
// Mark the cell as visited by setting it to 0.
// This prevents recounting and infinite loops.
grid[r][c] = 0;
// Count the current cell (area 1) and recursively explore its 4-directional neighbors
return 1 + dfs(grid, r + 1, c, m, n) + // Down
dfs(grid, r - 1, c, m, n) + // Up
dfs(grid, r, c + 1, m, n) + // Right
dfs(grid, r, c - 1, m, n); // Left
}
}
Max Area of Island in C++
class Solution {
public:
int maxAreaOfIsland(vector<vector<int>>& grid) {
int m = grid.size();
if (m == 0) return 0;
int n = grid[0].size();
int max_area = 0;
// Iterate through each cell of the grid
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
// If a land cell (1) is found, it's the start of a new island
if (grid[i][j] == 1) {
// Calculate the area of this island and update max_area
max_area = max(max_area, dfs(grid, i, j, m, n));
}
}
}
return max_area;
}
private:
// Helper function to perform Depth First Search (DFS)
int dfs(vector<vector<int>>& grid, int r, int c, int m, int n) {
// Base cases for recursion termination:
// 1. Check for out-of-bounds.
// 2. Check if the cell is water (0) or already visited.
if (r < 0 || r >= m || c < 0 || c >= n || grid[r][c] == 0) {
return 0;
}
// Mark the cell as visited by setting it to 0.
// This prevents recounting and infinite loops.
grid[r][c] = 0;
// Count the current cell (area 1) and recursively explore its 4-directional neighbors
return 1 + dfs(grid, r + 1, c, m, n) + // Down
dfs(grid, r - 1, c, m, n) + // Up
dfs(grid, r, c + 1, m, n) + // Right
dfs(grid, r, c - 1, m, n); // Left
}
};
Max Area of Island in TypeScript
function maxAreaOfIsland(grid: number[][]): number {
if (!grid || grid.length === 0) {
return 0;
}
const m = grid.length;
const n = grid[0].length;
let maxArea = 0;
const dfs = (r: number, c: number): number => {
// Base case: if out of bounds or water/visited cell, return 0 area.
if (r < 0 || r >= m || c < 0 || c >= n || grid[r][c] === 0) {
return 0;
}
// Mark the current land cell as visited by sinking it (1 -> 0).
grid[r][c] = 0;
// The area is 1 (for the current cell) + area of connected neighbors.
return (1 + dfs(r + 1, c) + // Down
dfs(r - 1, c) + // Up
dfs(r, c + 1) + // Right
dfs(r, c - 1)); // Left
};
// Iterate through each cell of the grid.
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
// If we find an unvisited land cell, start DFS to find its area.
if (grid[i][j] === 1) {
maxArea = Math.max(maxArea, dfs(i, j));
}
}
}
return maxArea;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.