N-Queens II — Rust Coding Problem
Difficulty: hard | Category: backtracking
Problem Description
The N-Queens puzzle places `n` queens on an `n x n` chess board so that no two queens attack each other. Given an integer `n`, return the number of distinct solutions. Hint: Backtracking. For each row, try placing a queen in each column. Use three sets to track which columns, and which diagonals (`row - col` and `row + col`) are already occupied. Count the arrangements where all `n` rows have been placed.
Examples
Example 1
Input: n = 4
Output: 2
Explanation: There are two distinct solutions to the 4-queens puzzle.
Example 2
Input: n = 1
Output: 1