NQueens


N-Queens – LeetCode


Problem: N-Queens

Problem Statement

The N-Queens puzzle is the problem of placing n chess queens on an n x n chessboard such that no two queens attack each other.

Return all distinct solutions to the n-queens puzzle.
You may return the answer in any order.

Each solution contains a distinct board configuration of the placement of the n queens, where 'Q' and '.' indicate a queen and an empty space respectively.


Examples

Example 1
Input: n = 4
Output:

[
 [".Q..",
  "...Q",
  "Q...",
  "..Q."],

 ["..Q.",
  "Q...",
  "...Q",
  ".Q.."]
]

Example 2
Input: n = 1
Output: [["Q"]]


Constraints


Intuition

This is a classical backtracking problem:


Approach (Backtracking with Board Validation)

  1. Use a 2D boolean array chess[n][n] to represent the board.

  2. Use recursion to place a queen row by row.

  3. At each row:

    • Try placing the queen in all columns.

    • If isValid() returns true, proceed.

  4. Base case: If row == n, convert the board to a list of strings and add to result.

  5. Backtrack after the recursive call by removing the queen.


Code (Java)

class Solution {
    public List<List<String>> solveNQueens(int n) {
        boolean[][] chess = new boolean[n][n];
        List<List<String>> ans = new ArrayList<>();
        helper(0, n, chess, ans);
        return ans;
    }

    public void helper(int row, int n, boolean[][] chess, List<List<String>> result) {
        if (row == n) {
            List<String> mat = new ArrayList<>();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < n; i++) {
                sb.setLength(0);
                for (int j = 0; j < n; j++) {
                    sb.append(chess[i][j] ? 'Q' : '.');
                }
                mat.add(sb.toString());
            }
            result.add(mat);
            return;
        }

        for (int col = 0; col < n; col++) {
            if (isValid(chess, row, col)) {
                chess[row][col] = true;
                helper(row + 1, n, chess, result);
                chess[row][col] = false; // backtrack
            }
        }
    }

    public boolean isValid(boolean[][] chess, int r, int c) {
        // Check vertically upwards
        for (int i = r - 1; i >= 0; i--) {
            if (chess[i][c]) return false;
        }

        // Check upper-left diagonal
        for (int i = r - 1, j = c - 1; i >= 0 && j >= 0; i--, j--) {
            if (chess[i][j]) return false;
        }

        // Check upper-right diagonal
        for (int i = r - 1, j = c + 1; i >= 0 && j < chess.length; i--, j++) {
            if (chess[i][j]) return false;
        }

        return true;
    }
}

Time and Space Complexity


Dry Run

Input: n = 4

Initial state: chess = 4x4 board, all false.

Start placing queen row by row:

Eventually, two configurations are found.


Conclusion

This problem is an elegant application of recursive backtracking combined with board validation logic. It teaches how to prune invalid paths early and how to build up state (chess board) and backtrack it efficiently.