Back to Practice Dashboard
Top 150 InterviewEasy

Max Area of Island

Learn how to solve the 'Max Area of Island' problem. This detailed resource details brute force and optimized approaches.

Problem Statement

Easy

You are given an m x n binary matrix grid. An island is a group of 1s (representing land) connected 4-directionally (horizontal or vertical). You may assume all four edges of the grid are surrounded by water.

The area of an island is the number of cells with a value 1 in the island.

Return the maximum area of an island in grid. If there is no island, return 0.

Write a function maxAreaOfIsland(grid: List[List[int]]) -> int.

Constraints
  • m == len(grid)
  • n == len(grid[i])
  • 1 <= m, n <= 50
  • grid[i][j] is 0 or 1

Examples

Example 1
Input
grid = [[0,0,1,0,0,0,0,1,0,0,0,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,1,1,0,1,0,0,0,0,0,0,0,0],[0,1,0,0,1,1,0,0,1,0,1,0,0],[0,1,0,0,1,1,0,0,1,1,1,0,0],[0,0,0,0,0,0,0,0,0,0,1,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,0,0,0,0,0,0,1,1,0,0,0,0]]
Output
6
Explanation

The maximum area of an island is 6, located in the lower-right part of the grid.

Example 2
Input
grid = [[0,0,0,0,0,0,0,0]]
Output
0
Explanation

There are no islands in the grid, so the max area is 0.

Need a Hint?
Represent graph node connections as an adjacency list/matrix, then use standard BFS or DFS graph traversal.
Edge Cases to Watch
  • Empty list or null input variables
  • Single item lists/arrays
  • Extremely large input bounds causing integer or stack overflow

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor