51. N-Queens LeetCode Solution
In this guide, you will get 51. N-Queens LeetCode Solution with the best time and space complexity. The solution to N-Queens problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- N-Queens solution in C++
- N-Queens solution in Java
- N-Queens solution in Python
- Additional Resources

Problem Statement of N-Queens
The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other.
Given an integer n, 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 n-queens’ placement, where ‘Q’ and ‘.’ both indicate a queen and an empty space, respectively.
Example 1:
Input: n = 4
Output: [[“.Q..”,”…Q”,”Q…”,”..Q.”],[“..Q.”,”Q…”,”…Q”,”.Q..”]]
Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above
Example 2:
Input: n = 1
Output: [[“Q”]]
Constraints:
1 <= n <= 9
Complexity Analysis
- Time Complexity: O(n \cdot n!)
- Space Complexity: |\texttt{ans}|
51. N-Queens LeetCode Solution in C++
class Solution {
public:
vector<vector<string>> solveNQueens(int n) {
vector<vector<string>> ans;
dfs(n, 0, vector<bool>(n), vector<bool>(2 * n - 1), vector<bool>(2 * n - 1),
vector<string>(n, string(n, '.')), ans);
return ans;
}
private:
void dfs(int n, int i, vector<bool>&& cols, vector<bool>&& diag1,
vector<bool>&& diag2, vector<string>&& board,
vector<vector<string>>& ans) {
if (i == n) {
ans.push_back(board);
return;
}
for (int j = 0; j < n; ++j) {
if (cols[j] || diag1[i + j] || diag2[j - i + n - 1])
continue;
board[i][j] = 'Q';
cols[j] = diag1[i + j] = diag2[j - i + n - 1] = true;
dfs(n, i + 1, std::move(cols), std::move(diag1), std::move(diag2),
std::move(board), ans);
cols[j] = diag1[i + j] = diag2[j - i + n - 1] = false;
board[i][j] = '.';
}
}
};
/* code provided by PROGIEZ */
51. N-Queens LeetCode Solution in Java
class Solution {
public List<List<String>> solveNQueens(int n) {
List<List<String>> ans = new ArrayList<>();
char[][] board = new char[n][n];
for (int i = 0; i < n; ++i)
Arrays.fill(board[i], '.');
dfs(n, 0, new boolean[n], new boolean[2 * n - 1], new boolean[2 * n - 1], board, ans);
return ans;
}
private void dfs(int n, int i, boolean[] cols, boolean[] diag1, boolean[] diag2, char[][] board,
List<List<String>> ans) {
if (i == n) {
ans.add(construct(board));
return;
}
for (int j = 0; j < cols.length; ++j) {
if (cols[j] || diag1[i + j] || diag2[j - i + n - 1])
continue;
board[i][j] = 'Q';
cols[j] = diag1[i + j] = diag2[j - i + n - 1] = true;
dfs(n, i + 1, cols, diag1, diag2, board, ans);
cols[j] = diag1[i + j] = diag2[j - i + n - 1] = false;
board[i][j] = '.';
}
}
private List<String> construct(char[][] board) {
List<String> listBoard = new ArrayList<>();
for (int i = 0; i < board.length; ++i)
listBoard.add(String.valueOf(board[i]));
return listBoard;
}
}
// code provided by PROGIEZ
51. N-Queens LeetCode Solution in Python
class Solution:
def solveNQueens(self, n: int) -> list[list[str]]:
ans = []
cols = [False] * n
diag1 = [False] * (2 * n - 1)
diag2 = [False] * (2 * n - 1)
def dfs(i: int, board: list[int]) -> None:
if i == n:
ans.append(board)
return
for j in range(n):
if cols[j] or diag1[i + j] or diag2[j - i + n - 1]:
continue
cols[j] = diag1[i + j] = diag2[j - i + n - 1] = True
dfs(i + 1, board + ['.' * j + 'Q' + '.' * (n - j - 1)])
cols[j] = diag1[i + j] = diag2[j - i + n - 1] = False
dfs(0, [])
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.