All problemsBack
Max Area of Island
mediumYou are given an m x n binary matrix grid. An island is a group of 1's (representing land) connected 4-directionally. You may assume all four edges of the grid are surrounded by water.
The area of an island is the number of cells with value 1 in the island.
Return the maximum area of an island in grid. If there is no island, return 0.
Test Cases
Copy an input into themain harness and Run to verifyInput
grid = [[0,0,1,0],[1,1,1,0],[0,1,0,0]]Expected Output
5Explanation: The connected block of 1's has area 5.
Input
grid = [[0,0,0],[0,0,0]]Expected Output
0Constraints
- m == grid.length
- n == grid[i].length
- 1 <= m, n <= 50
- grid[i][j] is either 0 or 1.
Hints
Hint 1 — click to reveal
This is Number of Islands, except each flood fill returns a size instead of just counting.
Hint 2 — click to reveal
Sinking each visited cell to 0 is a neat way to avoid a separate visited array.
Java Compiler
Powered by OneCompiler. Starter code loads automatically — edit and hit Run.