g0001_0100.s0051_n_queens.complexity.md Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java21 Show documentation
Show all versions of leetcode-in-java21 Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
**Time Complexity (Big O Time):**
1. The program uses a recursive backtracking algorithm to explore all possible solutions. It places queens row by row, and for each row, it iterates through the columns to find a valid placement.
2. The main recursive function `helper` is called for each row, and for each row, it iterates through the columns. In the worst case, this results in exploring all possible combinations of queen placements.
3. For each queen placement, it checks whether it's valid based on the positions of previously placed queens. The checks include validating the columns, diagonals, and anti-diagonals.
4. The time complexity of the recursive algorithm can be analyzed as follows:
- For the first row, there are N possibilities.
- For the second row, there are N-2 possibilities (two columns in the same column as the first queen are not allowed).
- For the third row, there are N-4 possibilities (four columns are eliminated due to diagonal and anti-diagonal constraints), and so on.
5. The worst-case time complexity of exploring all possible combinations is exponential, and it's typically represented as O(N!) for the N-Queens problem, where N is the size of the chessboard (number of rows and columns).
6. Additionally, constructing the solution in the `construct` method takes O(N) time for each solution.
7. Therefore, the overall time complexity is dominated by the number of recursive calls and is O(N!).
**Space Complexity (Big O Space):**
1. The space complexity is determined by the additional data structures used in the program.
2. The `pos` boolean array of size `n + 2 * n - 1 + 2 * n - 1` is used to keep track of occupied columns, diagonals, and anti-diagonals. It grows with the size of the chessboard but is not dependent on the number of solutions. Therefore, its space complexity is O(N).
3. The `pos2` integer array of size `n` is used to keep track of the column positions of queens in each row. Its space complexity is O(N).
4. The `ans` list of lists stores the solutions, and its space complexity depends on the number of solutions. In the worst case, there can be N! solutions.
5. Therefore, the overall space complexity is dominated by the storage of solutions and is O(N!).
In summary, the time complexity of the provided program is O(N!) due to the combinatorial nature of the N-Queens problem, and the space complexity is O(N) due to the additional data structures used to track queen placements and store solutions.