LeetCode 52, Hard. Topics: Backtracking. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 11 parametrized pytest cases, and a playground notebook:
Problem
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 the number of distinct solutions to the n-queens puzzle.
Examples
Constraints
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
NeetCode 250, NeetCode All. Last modified on September 7, 2026