-
Notifications
You must be signed in to change notification settings - Fork 0
Implement numIslands function to count islands in a grid #93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -36,8 +36,30 @@ def numIslands(self, grid): | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+41
to
+54
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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) |
Copilot
AI
Feb 19, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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).
Copilot
AI
Feb 19, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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).