695. Max Area of Island
All prompts are owned by LeetCode. To view the prompt, click the title link above.
First completed : June 24, 2024
Last updated : June 24, 2024
Related Topics : Array, Depth-First Search, Breadth-First Search, Union Find, Matrix
Acceptance Rate : 72.68 %
int getAreaAndVoid(int** grid, int gridSize, int* gridColSize, int x, int y) {
if (x < 0 || y < 0 || x >= gridSize || y >= gridColSize[0] || grid[x][y] == 0) {
return 0;
}
grid[x][y] = 0;
return 1 + getAreaAndVoid(grid, gridSize, gridColSize, x + 1, y)
+ getAreaAndVoid(grid, gridSize, gridColSize, x - 1, y)
+ getAreaAndVoid(grid, gridSize, gridColSize, x, y + 1)
+ getAreaAndVoid(grid, gridSize, gridColSize, x, y - 1);
}
int maxAreaOfIsland(int** grid, int gridSize, int* gridColSize) {
int max = 0;
for (int i = 0; i < gridSize; i++) {
for (int j = 0; j < gridColSize[0]; j++) {
if (grid[i][j]) {
int islandArea = getAreaAndVoid(grid, gridSize, gridColSize, i, j);
max = (islandArea > max) ? islandArea : max;
}
}
}
return max;
}