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.
Example 1
Input: n = 4
Output: 2
Explanation: A 4 x 4 board has exactly two ways to place 4 non-attacking queens, and they are mirror images of each other.
Example 2
Input: n = 1
Output: 1
Explanation: A 1 x 1 board has one arrangement: a single queen.
Example 3
Input: n = 2
Output: 0
Explanation: On a 2 x 2 board every pair of squares shares a row, column or diagonal, so there are 0 solutions.
Place one queen per row. For each row try every column that isn't already attacked; track occupied columns and both diagonals in sets for O(1) checks. Recurse to the next row, and count a solution each time all n rows are filled.
Run checks all cases above. Submit evaluates all test cases.