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
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.
- •m == len(grid)
- •n == len(grid[i])
- •1 <= m, n <= 50
- •grid[i][j] is 0 or 1
Examples
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]]
6
The maximum area of an island is 6, located in the lower-right part of the grid.
grid = [[0,0,0,0,0,0,0,0]]
0
There are no islands in the grid, so the max area is 0.
Need a Hint?
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.