Can anyone tell Why I am getting tle ?

Правка en4, от coder_of_india., 2021-03-27 22:04:38

*****************************************************TLE CODE **************************************************************** int dx[4] = {0 , -1 , 0 , 1} ; int dy[4] = {-1 , 0 , 1 , 0} ; bool checker(vector<vector> grid , int i , int j , int n , int m ) { if(i >= 0 && i < n && j >= 0 && j < m && ( grid[i][j] == '1')) return true; return false; } void dfs(vector<vector> &grid , int i , int j ,int r , int c) {

grid[i][j] = '0';
for(int k = 0 ; k < 4 ; k ++ )
{
    int newx = i + dx[k] ;
    int newy = j + dy[k] ;
    if( checker(grid , newx , newy , r , c)  ) 
    {    
        grid[newx][newy] = '0';
        dfs(grid , newx , newy , r , c);
    }
}

} class Solution { public: int numIslands(vector<vector>& grid) {

int r = grid.size() ;
    int c = grid[0].size() ;
    int cnt = 0 ;
    for(int i = 0 ; i < r ; i ++ )
    {
        for(int j = 0 ; j < c ; j ++ ) 
        {
              if(grid[i][j] == '1') 
              {
                  cnt ++ ; 
                  dfs(grid , i , j ,r , c);
              }
        }
    }
    return cnt ;
}

}; ************************************************** AC CODE ******************************************************************* int dx[4] = {0 , -1 , 0 , 1} ; int dy[4] = {-1 , 0 , 1 , 0} ; void dfs(vector<vector> &grid , int i , int j ,int r , int c) {

grid[i][j] = '0';
for(int k = 0 ; k < 4 ; k ++ )
{
    int newx = i + dx[k] ;
    int newy = j + dy[k] ;
    if(newx >= 0 && newx < r && newy >= 0 && newy < c && ( grid[newx][newy] == '1')) 
    {    
        grid[newx][newy] = '0';
        dfs(grid , newx , newy , r , c);
    }
}

} class Solution { public: int numIslands(vector<vector>& grid) {

int r = grid.size() ;
    int c = grid[0].size() ;
    int cnt = 0 ;
    for(int i = 0 ; i < r ; i ++ )
    {
        for(int j = 0 ; j < c ; j ++ ) 
        {
              if(grid[i][j] == '1') 
              {
                  cnt ++ ; 
                  dfs(grid , i , j ,r , c);
              }
        }
    }
    return cnt ;
}

}; ****************************************************************************************************************************** Both codes are same but there is one difference I used cheacker function that's why i am getting tle why this is happening ? please tell me anyone.

Thanks in advance !!

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en10 Английский coder_of_india. 2021-03-27 22:30:03 17 Tiny change: ' /// this funtion\n retu' -> ' \n retu'
en9 Английский coder_of_india. 2021-03-27 22:12:58 25 Tiny change: ' .\nbool che' -> ' \nbool che'
en8 Английский coder_of_india. 2021-03-27 22:11:05 276
en7 Английский coder_of_india. 2021-03-27 22:09:17 55
en6 Английский coder_of_india. 2021-03-27 22:07:05 194
en5 Английский coder_of_india. 2021-03-27 22:05:42 14
en4 Английский coder_of_india. 2021-03-27 22:04:38 41
en3 Английский coder_of_india. 2021-03-27 22:03:15 16
en2 Английский coder_of_india. 2021-03-27 22:02:13 2531
en1 Английский coder_of_india. 2021-03-27 21:57:05 367 Initial revision (published)