Skip to content

Backtracking-3 complete#1086

Open
Tanmay-Nalawade wants to merge 2 commits intosuper30admin:masterfrom
Tanmay-Nalawade:master
Open

Backtracking-3 complete#1086
Tanmay-Nalawade wants to merge 2 commits intosuper30admin:masterfrom
Tanmay-Nalawade:master

Conversation

@Tanmay-Nalawade
Copy link
Copy Markdown

No description provided.

@super30admin
Copy link
Copy Markdown
Owner

N-Queens (51. N-Queens.py)

Note: The verdict should be based on whether the solution is correct and efficient enough to pass all test cases on a typical programming judge.

Lets begin.

VERDICT: NEEDS_IMPROVEMENT


Word Search (79. Word Search.py)

Your solution is close to being correct and efficient. Here are some points to consider:

Strengths:

  • You have correctly implemented the backtracking DFS approach.
  • You correctly mark the visited cell with '#' and backtrack by restoring the value.
  • You have provided good comments and time/space complexity analysis.

Areas for improvement:

  1. The base condition in the DFS function should include a check for visited cells (i.e., if the cell is marked as '#') to avoid processing it. Currently, you only check boundaries and then the character. While it works because the word doesn't contain '#', it is good practice to explicitly check for visited cells to avoid confusion and to make the code more robust. Also, this check should be done early to avoid unnecessary work.

  2. You are making recursive calls without checking if the next cell is within bounds or visited. This leads to many unnecessary function calls. You should check the conditions before the recursive call to prune the search early. For example, inside the loop for directions, you should check:
    if 0 <= nr < m and 0 <= nc < n and board[nr][nc] != '#':
    if self.isWord(...): return True

    This will prevent recursive calls for out-of-bound indices and visited cells.

  3. The function name "isWord" is not very descriptive. Consider renaming it to "dfs" or "search" to better reflect its purpose.

  4. You are passing many parameters (board, word, m, n, direc) repeatedly. This can be avoided by making them instance variables or using a nested function that has access to these variables. This would make the code cleaner.

  5. In the main loop, you are returning immediately when found is True. This is good. But note: the variable "found" is actually a boolean, and you are returning it. You can directly return the result of isWord if it is True, without storing it.

Revised code suggestion:

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
m, n = len(board), len(board[0])
directions = [(1,0), (0,1), (-1,0), (0,-1)]

    def dfs(idx, row, col):
        if idx == len(word):
            return True
        if row < 0 or row >= m or col < 0 or col >= n or board[row][col] == '#':
            return False
        if board[row][col] != word[idx]:
            return False
        # Mark as visited
        temp = board[row][col]
        board[row][col] = '#'
        for dr, dc in directions:
            nr, nc = row + dr, col + dc
            if dfs(idx+1, nr, nc):
                return True
        board[row][col] = temp
        return False
    
    for i in range(m):
        for j in range(n):
            if dfs(0, i, j):
                return True
    return False

This revised code checks boundaries and visited state at the beginning of DFS, which is more efficient. It also uses a nested function to avoid passing repeated parameters.

VERDICT: NEEDS_IMPROVEMENT

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.

2 participants