Skip to main content
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

  • 1 <= n <= 9

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

NeetCode 250, NeetCode All.
Last modified on August 25, 2026