Skip to content

Add implementation for leetcode problems 51, 79#1084

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

Add implementation for leetcode problems 51, 79#1084
rishigoswamy wants to merge 1 commit intosuper30admin:masterfrom
rishigoswamy:master

Conversation

@rishigoswamy
Copy link

No description provided.

@super30admin
Copy link
Owner

N-Queens (leetcode_51.py)

Your solution is excellent! You've implemented an efficient backtracking algorithm with O(1) constraint checks using boolean arrays. This is a common optimization for the N-Queens problem and is more efficient than the reference solution which checks constraints in O(n) time.

Strengths:

  • You correctly identified and implemented the constraint propagation using three boolean arrays.
  • The code is well-commented and structured, making it easy to understand.
  • You used meaningful variable names, which enhances readability.

Areas for Improvement:

  1. The columnFilled array is initialized with size n+1, but indices from 0 to n-1 are used. You can change the size to n to be more precise.
  2. Similarly, mainDiagonalFilled and antiDiagonalFilled are initialized with size 2*n, which is correct because the diagonals range from 0 to 2n-1. However, note that mainDiagonalIndex = row - col + n ranges from 0 (when row=0, col=n-1) to 2n-1 (when row=n-1, col=0). So the size is correct.
  3. You can avoid using the 2D board array for tracking placements during backtracking. Instead, you can store the column placement for each row (e.g., in an array cols of length n where cols[row] = col). Then, when you reach a solution, you can generate the board strings from this array. This would reduce the space usage from O(n^2) to O(n) for the board state during backtracking. However, the output still requires O(n^2) per solution, so overall space complexity remains similar.
  4. Consider making the helper function a private method (e.g., _solve) if following Python conventions, but this is minor.

Overall, your solution is correct and efficient. Great job!

VERDICT: PASS


Word Search (leetcode_79.py)

Strengths:

  • You have implemented a correct DFS backtracking solution.
  • The frequency pre-check optimization is excellent and can save time in cases where the word has characters not present in sufficient quantity in the board.
  • The code is well-commented and structured, making it easy to understand.
  • You explored multiple approaches (DFS with and without pre-check, and BFS), which shows good problem-solving curiosity.

Areas for Improvement:

  • The BFS approach is inefficient in terms of memory because it stores a visited set for each path. For this problem, DFS with backtracking is more suitable due to lower memory usage.
  • While it's good to have multiple implementations, in a production environment, it's better to focus on the most efficient one. You could remove the commented-out alternative implementations to keep the code clean.
  • The frequency pre-check uses a defaultdict. However, if the word contains a character not in the board, the defaultdict will return 0 and then decrement to -1, which is correct. But note that the pre-check might not be necessary for small boards (as per constraints) but is a good practice for larger inputs.
  • Ensure that the main function is the first one defined. In your code, the main exist function is defined first, which is good.

Overall, your solution is correct and efficient. The pre-check is a nice touch.

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