Skip to content

working solution#1083

Open
avcode3 wants to merge 1 commit intosuper30admin:masterfrom
avcode3:master
Open

working solution#1083
avcode3 wants to merge 1 commit intosuper30admin:masterfrom
avcode3:master

Conversation

@avcode3
Copy link

@avcode3 avcode3 commented Mar 14, 2026

No description provided.

@super30admin
Copy link
Owner

N-Queens (problem1.py)

Your solution has the right approach using backtracking. However, there is a critical error in the isValid method: it does not check for array indices being within bounds. For example, when checking the left diagonal, if the column index becomes negative, it will cause an IndexError. You need to ensure that when you are checking the diagonals, you only consider indices that are within [0, n-1].

To fix this, you should add bounds checks in the while loops. For instance, in the left diagonal check, you should only continue while r >= 0 and c >= 0. Similarly, for the right diagonal, while r >= 0 and c < n. Actually, you already have these conditions in the while loops? Let me look again:

while r >=0 and c>=0:   # for left diagonal
while r>=0 and c < n:

VERDICT: NEEDS_IMPROVEMENT


Word Search (problem2.py)

Strengths:

  • The solution correctly implements the DFS backtracking algorithm.
  • The code is concise and easy to follow.
  • The use of a directions list for neighbors is good.

Areas for Improvement:

  1. Variable Naming: Consider using more descriptive names for variables. For example, dir_s could be renamed to dir or direction.
  2. Instance Variables: Instead of storing m, n, and word as instance variables (using self), you can pass them as parameters or use local variables. This reduces statefulness and makes the code more functional and easier to reason about. However, for recursion, passing extra parameters might increase stack space slightly, but it's generally acceptable.
  3. Edge Cases: The solution handles edge cases well, but it's always good to consider adding a check for an empty board or empty word. However, the constraints say that m, n >= 1 and word.length >= 1, so it's not strictly necessary.
  4. Consistency: The code uses a mix of instance variables and parameters. It would be more consistent to either use all instance variables or pass all necessary data as parameters. Since board is passed as a parameter, it might be better to pass m and n as well or compute them inside helper from board (though that would be inefficient). Alternatively, you can compute m and n once in exist and pass them to helper as parameters.

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 False

This refactoring uses a nested function for DFS, which avoids the need for self in the helper and makes the code cleaner. It also uses more descriptive variable names.

VERDICT: PASS

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