The N-Queens puzzle is placing n queens on an n×n chessboard so no two queens attack each other. Return the number of distinct solutions.
Same backtracking as N-Queens but only count solutions instead of building board strings. Track occupied columns and diagonals using three boolean arrays.
- Use three boolean arrays: cols[], diag1[] (row-col+n), diag2[] (row+col).
- At each row, try placing a queen in each column if not under attack.
- Mark the column and diagonals, recurse to next row, then unmark.
- Increment counter when all n rows are filled.
- Time Complexity: O(N!) with pruning
- Space Complexity: O(N) — boolean arrays + stack