text function numIslands(grid): count = 0 for i from 0 to rows-1: for j from 0 to cols-1: if grid[i][j] == '1': count++ dfs(grid, i, j) return count function dfs(grid, i, j): if i < 0 or i >= rows or j < 0 or j >= cols: return if grid[i][j] != '1': return grid[i][j] = '0' // mark as visited dfs(grid, i+1, j) dfs(grid, i-1, j) dfs(grid, i, j+1) dfs(grid, i, j-1) Time complexity: where = rows, = cols. Space complexity: for the recursion stack in the worst case.
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
$ curl repovive.com/roadmaps/maang-interview-prep/graphs/number-of-islands-pseudocode
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█████████████████████████████████████████████████████████████████████████████████████