Memory Limit Exceeded in C++


  • 0
    G

    All, I've good a Memory Limit Exceeded message in my sumission, However I am not using any extra memory structures. Can any one spot the problem:

    class Solution {
    public:
    int islandPerimeter(vector<vector<int>>& grid) {
    int p=0;
    for(unsigned int i = 0; i< grid.size() ; ++i){
    for(unsigned int j = 0; j< grid[i].size() ; ++j){
    if(!grid[i][j]){ continue; }
    if( (i==0) || (!grid[i-1][j]) ){p++;}
    if( (i==grid.size()-1) || (!grid[i+1][j]) ){p++;}
    if( (j==0) || (!grid[i][j-1]) ){p++;}
    if( (j==grid[i].size()-1) || (!grid[i][j+1]) ){p++;}
    }
    }
    return p;
    }
    };


  • 0
    T

    I have the same issue with a similar solution... still can't figure out why


  • 0
    X

    I use C and met this problem too :(
    '''
    int islandPerimeter(int** grid, int gridRowSize, int gridColSize) {

    int edge = 0;
    int repeat = 0;
    
    for(int row = 0; row < gridRowSize; row++) {
        for(int col = 0; col < gridColSize; col++){
            if(grid[row][col] == 1){
                edge += 4;
                if(row!=0&&grid[row-1][col] == 1){
                    repeat += 2;
                }
                if(col!=0&&grid[row][col-1] == 1){
                    repeat += 2;
                }
            }
        }
    }
    
    return edge - repeat;
    

    }
    '''


  • 0
    G

    It looks like the testcase that is checking for the maximum memory limit in C/C++ is wrong. Does anyone knows how we can report it?


  • 0

    This issue was just fixed for both C/C++.


Log in to reply
 

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