Are either of these more efficient or standard/readable? The second option seems to do less operations
def dfs(row, col):
if row >= len(grid) or row < 0 or col >= len(grid[row]) or col < 0 or not visited[row, col]:
return
visited[row, col] = False
dfs(row + 1, col)
dfs(row - 1, col)
dfs(row, col + 1)
dfs(row, col - 1)
def dfs(row, col):
visited[row, col] = False
if row < len(grid) - 1 and not visited[row + 1][col]:
dfs(row + 1, col)
if row > 0 and not visited[row - 1][col]:
dfs(row - 1, col)
if col < len(grid[row]) - 1 and not visited[row][col + 1]:
dfs(row, col + 1)
if col > 0 and not visited[row][col - 1]:
dfs(row, col - 1)
Which one would you write and why?
New contributor
Sameem is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.