Practice/Amazon/Leetcode 695. Max Area of Island
CodingMust
You are given a 2D grid where each cell contains either 0 (water) or 1 (land). An island is defined as a group of land cells that are connected horizontally or vertically (4-directional connectivity). Diagonal connections do not count.
Your task is to find the area of the largest island in the grid. The area of an island is the number of land cells it contains. If there are no islands in the grid, return 0.
m == grid.length (number of rows)n == grid[i].length (number of columns)1 <= m, n <= 50grid[i][j] is either 0 or 1Example 1:
Input: grid = [ [1,1,0,0,0], [1,1,0,0,0], [0,0,0,1,1], [0,0,0,1,1] ] Output: 4 Explanation: There are two islands with areas 4 and 4. The maximum is 4.
Example 2:
Input: grid = [ [0,0,0], [0,0,0] ] Output: 0 Explanation: No land cells exist, so the answer is 0.
Example 3:
Input: grid = [ [1,1,1,0,0], [0,0,1,0,1], [0,0,1,1,1] ] Output: 6 Explanation: The center/right island has cells at positions forming a connected component of size 6.