Skip to content

Backtracking-3#1090

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

Backtracking-3#1090
paridhimalviya wants to merge 1 commit intosuper30admin:masterfrom
paridhimalviya:master

Conversation

@paridhimalviya
Copy link
Copy Markdown

@super30admin
Copy link
Copy Markdown
Owner

N-Queens (N-Queens.swift)

  1. The code correctly implements the backtracking algorithm for n-queens, generating all valid configurations. The base case properly constructs the solution string when all rows are processed.
  2. The time complexity is optimal for this problem, matching the reference solution. The isValid function efficiently checks vertical and diagonal conflicts in O(n) time per call.
  3. The space usage is appropriate, using a boolean matrix to track queen placements and recursion stack for backtracking. This is standard for this problem.
  4. The code is readable with helpful comments explaining the backtracking approach and time complexity. However, the variable names could be more descriptive (e.g., 'matrix' could be 'board' for clarity).
  5. One minor improvement: The initial 'var s = ""' in solveNQueens is unused and should be removed to avoid confusion.

VERDICT: PASS


Word Search (WordSearch.swift)

Strengths:

  • You have implemented the DFS backtracking approach correctly in the existBoolBased function.
  • You have considered the base cases and boundary conditions.

Areas for Improvement:

  1. Avoid using global variables: The first solution (exist) uses a global isPresent variable which is not reset. This means that if you call the function multiple times, it might return true for a previous call. Instead, use the boolean return approach as in existBoolBased.
  2. Remove unused code: The visited array in the first solution is not used and should be removed. The in-place modification of the board is sufficient.
  3. Optimize the dirs array: Declare the dirs array as a constant (e.g., static) to avoid recreating it in every recursive call. This improves performance.
  4. Improve variable names: Use descriptive names like newRow and newCol instead of nr and nc.
  5. Consider Swift conventions: Use Swift's built-in types and conventions. For example, you can use (i, j) for indices, but note that Swift uses row and col commonly.
  6. The exist function should be removed since it is incorrect. Only the existBoolBased function should be kept.

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