Skip to content

Implement numIslands function to count islands in a grid#93

Open
cd155 wants to merge 1 commit intomainfrom
43-leetcode-200-number-of-islands
Open

Implement numIslands function to count islands in a grid#93
cd155 wants to merge 1 commit intomainfrom
43-leetcode-200-number-of-islands

Conversation

@cd155
Copy link
Owner

@cd155 cd155 commented Feb 19, 2026

This pull request implements the solution for counting the number of islands in a grid using depth-first search (DFS) in the numIslands function. The previously unimplemented method now tracks visited cells and recursively explores connected land cells.

Implementation of DFS-based island counting:

  • Added a DFS helper function to recursively explore connected land cells, marking them as visited in visit_record.
  • Updated the main logic to iterate through the grid, incrementing the island count for each unvisited land cell and invoking DFS to mark all connected cells.
  • Replaced the placeholder pass statement with the complete solution and returned the final count of islands.

Copilot AI review requested due to automatic review settings February 19, 2026 01:17
@cd155 cd155 linked an issue Feb 19, 2026 that may be closed by this pull request
4 tasks
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 pass placeholder.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 36 to +42
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
Copy link

Copilot AI Feb 19, 2026

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).

Copilot uses AI. Check for mistakes.
Comment on lines +56 to +57
for i in range(0, len(grid)):
for j in range(0, len(grid[0])):
Copy link

Copilot AI Feb 19, 2026

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).

Suggested change
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):

Copilot uses AI. Check for mistakes.
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)
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Suggested change
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 uses AI. Check for mistakes.
Comment on lines +44 to +54
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)
Copy link

Copilot AI Feb 19, 2026

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 uses AI. Check for mistakes.
@cd155 cd155 changed the title feat: implement numIslands function to count islands in a grid Implement numIslands function to count islands in a grid Feb 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LeetCode 200: Number of Islands

1 participant

Comments