Implement totalNQueens to count the distinct ways to place n queens on an n by n chessboard so that no two queens attack one another. Two queens attack one another if they share a row, column, or diagonal.
Every valid arrangement places exactly one queen in each row and each column. Arrangements are distinct when at least one queen occupies a different square.
Return the total number of valid arrangements as an integer. If no arrangement satisfies the rules, return 0.
Example 1:
Input: n = 2
Output: 0
Explanation: With only two rows and two columns, placing one queen in each row forces the queens to share a column or diagonal.
Example 2:
Input: n = 6
Output: 4
Explanation: There are four distinct placements of six queens in which no pair shares a row, column, or diagonal.
Example 3:
Input: n = 7
Output: 40
Explanation: A complete search of the seven-row board produces 40 valid arrangements.
Constraints:
1 <= n <= 9 is an integer.The board has nrows andn columns.The answer fits in a signed 32-bit integer.Need help or want more practice?