Error
|
|
---|---|
@Ibrahim Nash | 5761 |
@blackshadows | 5715 |
@akhayrutdinov | 5111 |
@mb1973 | 4989 |
@Quandray | 4944 |
@saiujwal13083 | 4506 |
@sanjay05 | 3762 |
@marius_valentin_dragoi | 3516 |
@sushant_a | 3459 |
@verma_ji | 3341 |
@KshamaGupta | 3318 |
Complete Leaderboard | |
|
|
@aroranayan999 | 1083 |
@bt8816103042 | 739 |
@SherlockHolmes3 | 444 |
@SHOAIBVIJAPURE | 430 |
@codeantik | 429 |
@shalinibhataniya1097 | 400 |
@ShamaKhan1 | 392 |
@neverevergiveup | 372 |
@amrutakashikar2 | 355 |
@murarry3625 | 350 |
@mahlawatep | 349 |
Complete Leaderboard |
The n-queens puzzle is the problem of placing n queens on a (n×n) chessboard such that no two queens can attack each other.
Given an integer n, find all distinct solutions to the n-queens puzzle. Each solution contains distinct board configurations of the n-queens’ placement, where the solutions are a permutation of [1,2,3..n] in increasing order, here the number in the ith place denotes that the ith-column queen is placed in the row with that number. For eg below figure represents a chessboard [3 1 4 2].
Example 1:
Input: 1
Output: [1]
Explaination: Only one queen can be placed
in the single cell available.
Example 2:
Input: 4
Output: [2 4 1 3 ] [3 1 4 2 ]
Explaination: These are the 2 possible solutions.
Your Task:
You do not need to read input or print anything. Your task is to complete the function nQueen() which takes n as input parameter and returns a list containing all the possible chessboard configurations. Return an empty list if no solution exists.
Expected Time Complexity: O(n!)
Expected Auxiliary Space: O(n2)
Constraints:
1 ≤ n ≤ 10
We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?
Yes