Implement numIslands function to count islands in a grid#93
Implement numIslands function to count islands in a grid#93
Conversation
There was a problem hiding this comment.
Pull request overview
Implements the previously placeholder numIslands method to count connected land components (“islands”) in a 2D grid using DFS traversal.
Changes:
- Added a DFS helper to traverse and mark connected land cells as visited.
- Added grid iteration logic to count islands by starting DFS from each unvisited land cell.
- Returned the computed island count instead of the prior
passplaceholder.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| Time Complexity: O(m * n) | ||
| Space Complexity: O(m * n) for recursion stack | ||
| """ | ||
| # TODO: Implement solution | ||
| pass | ||
| row_num = len(grid) | ||
| col_num = len(grid[0]) | ||
| visit_record = [] | ||
| num_island = 0 |
There was a problem hiding this comment.
The docstring's space complexity note says it's for the recursion stack, but the implementation also stores all visited cells in visit_record, which is O(m*n) additional space. Please update the complexity comment to match the actual approach (and if you move to iterative traversal, adjust accordingly).
| for i in range(0, len(grid)): | ||
| for j in range(0, len(grid[0])): |
There was a problem hiding this comment.
row_num/col_num are computed but the loops still use len(grid) / len(grid[0]). Using the already-computed variables avoids repeated length lookups and keeps the code consistent (also helps if you later add an early return for empty input).
| for i in range(0, len(grid)): | |
| for j in range(0, len(grid[0])): | |
| for i in range(row_num): | |
| for j in range(col_num): |
| visit_record = [] | ||
| num_island = 0 | ||
|
|
||
| def dfs(i, j): | ||
| if (i,j) not in visit_record and grid[i][j] == "1": | ||
| visit_record.append((i, j)) | ||
| if 0<=i-1<row_num: | ||
| dfs(i-1, j) | ||
| if 0<=i+1<row_num: | ||
| dfs(i+1, j) | ||
| if 0<=j-1<col_num: | ||
| dfs(i, j-1) | ||
| if 0<=j+1<col_num: | ||
| dfs(i, j+1) |
There was a problem hiding this comment.
visit_record is a list, so (i, j) not in visit_record is an O(k) linear scan. Since this check is done for every cell (and again within DFS), the worst-case runtime can degrade to ~O((mn)^2) and contradicts the stated O(mn) complexity. Use a set for visited (or a boolean matrix / in-place marking) so membership tests are O(1).
| visit_record = [] | |
| num_island = 0 | |
| def dfs(i, j): | |
| if (i,j) not in visit_record and grid[i][j] == "1": | |
| visit_record.append((i, j)) | |
| if 0<=i-1<row_num: | |
| dfs(i-1, j) | |
| if 0<=i+1<row_num: | |
| dfs(i+1, j) | |
| if 0<=j-1<col_num: | |
| dfs(i, j-1) | |
| if 0<=j+1<col_num: | |
| dfs(i, j+1) | |
| visit_record = set() | |
| num_island = 0 | |
| def dfs(i, j): | |
| if (i, j) not in visit_record and grid[i][j] == "1": | |
| visit_record.add((i, j)) | |
| if 0 <= i - 1 < row_num: | |
| dfs(i - 1, j) | |
| if 0 <= i + 1 < row_num: | |
| dfs(i + 1, j) | |
| if 0 <= j - 1 < col_num: | |
| dfs(i, j - 1) | |
| if 0 <= j + 1 < col_num: | |
| dfs(i, j + 1) |
| def dfs(i, j): | ||
| if (i,j) not in visit_record and grid[i][j] == "1": | ||
| visit_record.append((i, j)) | ||
| if 0<=i-1<row_num: | ||
| dfs(i-1, j) | ||
| if 0<=i+1<row_num: | ||
| dfs(i+1, j) | ||
| if 0<=j-1<col_num: | ||
| dfs(i, j-1) | ||
| if 0<=j+1<col_num: | ||
| dfs(i, j+1) |
There was a problem hiding this comment.
This recursive DFS can hit Python's recursion limit on worst-case inputs (constraints allow up to 300x300; a single large island can require tens of thousands of recursive calls), causing RecursionError and failing on valid inputs. Consider switching to an iterative DFS/BFS using an explicit stack/queue (or otherwise avoid deep recursion).
This pull request implements the solution for counting the number of islands in a grid using depth-first search (DFS) in the
numIslandsfunction. The previously unimplemented method now tracks visited cells and recursively explores connected land cells.Implementation of DFS-based island counting:
visit_record.passstatement with the complete solution and returned the final count of islands.