Hard · Backtracking
N-Queens
Given an integer n, place n queens on an n×n board so that no two share a row, column or diagonal, and return the lexicographically smallest such placement as the 0-based queen column of each row together with true, or null and false when no placement exists (n = 2 or 3).
Examples
Example 1
n = 4Output: [1, 3, 0, 2]
Example 2
n = 6Output: [1, 3, 5, 0, 2, 4]
Rebuild it in the studio
Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.