N-Queens II - Complete Solution Guide
N-Queens II is LeetCode problem 52, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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: There are two distinct solutions to the 4-queens puzzle as shown. Example 2: Input: n = 1 Output: 1 Constraints: 1 <= n <= 9
Detailed Explanation
The N-Queens II problem is a variation of the classic N-Queens problem. The goal is to determine the number of distinct ways to place 'n' queens on an 'n x n' chessboard such that no two queens threaten each other. This means no two queens can share the same row, column, or diagonal. The input is an integer 'n' representing the size of the board, and the output is the number of valid arrangements of queens.
Solution Approach
The solution uses a backtracking algorithm to explore all possible queen placements. It iterates through each row, trying to place a queen in each column. Before placing a queen, it checks if the current column and diagonals are safe (not occupied by another queen). If safe, it places the queen, marks the column and diagonals as occupied, and recursively calls the backtracking function for the next row. After the recursive call returns, it unmarks the column and diagonals (backtracks) to explore other possibilities. The algorithm counts the number of times all queens can be placed successfully.
Step-by-Step Algorithm
- Step 1: Initialize sets (or arrays in C) to track occupied columns, diagonals, and anti-diagonals.
- Step 2: Define a recursive `backtrack` function that takes the current row as input.
- Step 3: Base Case: If the current row equals 'n', it means all queens have been placed successfully. Increment the count and return.
- Step 4: Iterate through each column in the current row.
- Step 5: Calculate the diagonal and anti-diagonal indices based on the row and column.
- Step 6: Check if the current column, diagonal, and anti-diagonal are already occupied. If so, continue to the next column.
- Step 7: If the current position is safe, mark the column, diagonal, and anti-diagonal as occupied.
- Step 8: Recursively call the `backtrack` function for the next row (row + 1).
- Step 9: After the recursive call returns, unmark the column, diagonal, and anti-diagonal (backtrack) to explore other possibilities.
- Step 10: After exploring all columns, return from the `backtrack` function.
- Step 11: After the initial call to the `backtrack` function, return the final count of valid solutions.
Key Insights
- Insight 1: Backtracking is essential to explore all possible queen placements systematically.
- Insight 2: Efficiently track occupied columns and diagonals to avoid invalid placements using sets or arrays.
- Insight 3: Understand that the diagonals and anti-diagonals can be represented using simple arithmetic relationships between row and column indices (row - col for diagonals and row + col for anti-diagonals).
Complexity Analysis
Time Complexity: O(n!)
Space Complexity: O(n)
Topics
This problem involves: Backtracking.
Companies
Asked at: Amazon, Bloomberg, Google, Liftoff, Microsoft, Snowflake, Zenefits.