Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions src/graph/number_of_islands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines 36 to +42
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.

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

for i in range(0, len(grid)):
for j in range(0, len(grid[0])):
Comment on lines +56 to +57
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.
if (i,j) not in visit_record and grid[i][j] == "1":
num_island += 1
dfs(i, j)

return num_island


# Example usage (for testing locally)
Expand Down