Home
Log in
DescriptionSubmissionsSolution
DescriptionSubmissionsSolution
Loading...

Log in to run or submit

n =

Codey

Practise coding problems, test your solutions and track your progress.

Explore

  • Problems
  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

© 2026 Codey. Personal learning project.

Solution
Updated: 2026-02-23

Idea

Place one queen per row; track attacked columns and diagonals.

Approach

Backtrack row by row. For each row, try all columns that are not in:

  • cols
  • diag1 (r-c)
  • diag2 (r+c) Place queen, recurse to next row, then undo.

Why it works

Queens attack only columns and diagonals. Sets let us test legality in O(1).

Code
Loading...
Complexity
Time: O(n!) (pruned)
Space: O(n)
Solution
Updated: 2026-02-23

Idea

Place one queen per row; track attacked columns and diagonals.

Approach

Backtrack row by row. For each row, try all columns that are not in:

  • cols
  • diag1 (r-c)
  • diag2 (r+c) Place queen, recurse to next row, then undo.

Why it works

Queens attack only columns and diagonals. Sets let us test legality in O(1).

Code
Loading...
Complexity
Time: O(n!) (pruned)
Space: O(n)