Skip to content

Backtracking-3 Done#1075

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

Backtracking-3 Done#1075
vaishnavi2231 wants to merge 1 commit intosuper30admin:masterfrom
vaishnavi2231:master

Conversation

@vaishnavi2231
Copy link

No description provided.

@super30admin
Copy link
Owner

Strengths:

  • The N-Queens solution is correct and follows the standard backtracking approach.
  • The code is well-organized with helper functions for clarity.
  • The isSafe function correctly checks the column and both diagonals.

Areas for Improvement:

  • In the helper function for N-Queens, the parameter 'j' is unnecessary since it is not used. You can remove it and define j only in the inner loop.
  • Consider using a list of strings or a 1D array to represent the board to save space, but for n up to 9, the current approach is acceptable.
  • For the Word Search solution (if you want to improve), you can avoid the base case for a 1x1 board by handling it within the DFS. The current special case might not be necessary because the DFS should handle it.

Overall, both solutions are well-written and efficient.

@super30admin
Copy link
Owner

Your solution for N-Queens is correct and efficient. Well done! Here are a few suggestions to improve code clarity and avoid potential issues:

  1. In the helper function, you have a parameter j that is not used. The inner loop for j in range(n) defines its own j, so the parameter is unnecessary and can be removed. This will make the code cleaner and avoid confusion.

  2. Consider renaming mat to something more descriptive like board to make the code easier to understand.

  3. The docstring at the top mentions "Place the queen if safe, go to next row. if cannot place in next row, backtrack by making 1->0". However, in your code, you are using booleans (True/False) instead of 1/0. This is a minor inconsistency in the comment.

  4. The function isSafe is well-implemented, but note that the comment inside says "right check diagonal" for the upward-right diagonal, and "right left diagonal" for the upward-left diagonal. These comments are a bit confusing. You might simply say "top-right diagonal" and "top-left diagonal".

  5. Although not necessary for small n, you could optimize the isSafe function by using sets or arrays to track occupied columns and diagonals, reducing the check to O(1). This would improve the time complexity slightly.

Overall, your solution is solid and demonstrates a good understanding of backtracking.

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