Conversation
N-Queens (problem1.py)Your solution has the right approach using backtracking. However, there is a critical error in the To fix this, you should add bounds checks in the while loops. For instance, in the left diagonal check, you should only continue while VERDICT: NEEDS_IMPROVEMENT Word Search (problem2.py)Strengths:
Areas for Improvement:
Suggested Refactoring: class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
m = len(board)
n = len(board[0])
directions = [[-1,0],[0,-1],[1,0],[0,1]]
def dfs(i, j, idx):
if idx == len(word):
return True
if i < 0 or j < 0 or i >= m or j >= n or board[i][j] == '#':
return False
if word[idx] != board[i][j]:
return False
temp = board[i][j]
board[i][j] = '#'
for dx, dy in directions:
r = i + dx
c = j + dy
if dfs(r, c, idx+1):
return True
board[i][j] = temp
return False
for i in range(m):
for j in range(n):
if dfs(i, j, 0):
return True
return FalseThis refactoring uses a nested function for DFS, which avoids the need for VERDICT: PASS |
No description provided.