Skip to content

Latest commit

 

History

History
56 lines (40 loc) · 1.55 KB

_695. Max Area of Island.md

File metadata and controls

56 lines (40 loc) · 1.55 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


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 %


Solutions

C

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;

    
}