Resubmit the accepted solution get a "Memory Limit Exceeded" error


  • 0
    L
    int islandPerimeter(int** grid, int gridRowSize, int gridColSize) {
        int p = 0;
        int row = 0;
        int col = 0;
        for(row = 0; row < gridRowSize; row++) {
            for(col = 0; col < gridColSize; col++) {
                if(grid[row][col] == 1) {
                    if(row == 0) {
                        p++;
                    }
                    if((row) == (gridRowSize - 1)) {
                        p++;
                    }
                    if(col == 0) {
                        p++;
                    }
                    if((col) == (gridColSize - 1)) {
                        p++;
                    }
                    if((row-1 >= 0) && (grid[row-1][col] == 0)) {
                        p++;
                    }
                    if((row+1 < gridRowSize) && (grid[row+1][col] == 0)) {
                        p++;
                    }
                    if((col-1 >= 0) && (grid[row][col-1] == 0)) {
                        p++;
                    }
                    if((col+1 < gridColSize) && (grid[row][col+1] == 0)) {
                        p++;
                    }
                }
            }
        }
    
        return p;
    }
    

    alt text
    alt text

    and i submit the solution in the top solutions also get the same error.

    this is the solution in the top solutions:

    int islandPerimeter(int** grid, int gridRowSize, int gridColSize) {
        int count = 0, repeat = 0;
        for(int i = 0; i < gridRowSize; i++)
        {
            for(int j = 0; j < gridColSize; j++)
                {
                    if(grid[i][j] == 1)
                    {
                        count++;
                        if(i != 0 && grid[i-1][j] == 1) repeat++;
                        if(j != 0 && grid[i][j-1] == 1) repeat++;
                    }
                }
        }
        return 4 * count - repeat * 2;
    }
    

    any one can help me?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.