Zero To DSAZero To DSA
Privacy Policy
Generate Parentheses

N-Queens

hard
Time: O(n!)
Space: O(n²)

The n-queens puzzle: place n queens on an n×n board such that no two queens attack each other. Return all distinct solutions.

Constraints

  • 1 <= n <= 9

Examples

Input: n = 4
Output: [[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]
Input: n = 1
Output: [["Q"]]