N-Queens II

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.

A queen can attack another queen if they share the same row, column, or diagonal.

Example 1
Inputn = 4
Output2
There are exactly two distinct ways to place 4 queens on a 4 x 4 board so that none attack each other.
Example 2
Inputn = 1
Output1
A single queen can be placed on the only square of a 1 x 1 board.

Constraints

  • 1 <= n <= 9

Asked at 10 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate