N Queens

Hard

Question

You have an n by n chessboard. Place n queens on it so that no two queens attack each other. That means no two queens can share a row, a column, or a diagonal.

Return every possible way to place the queens.

Note: "Q" marks a queen and "." marks an empty square.

Input: n = 4

Output: [[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]

There are two ways to place 4 queens:

N Queens Example 1

Input: n = 1

Output: [["Q"]]

There's only one way to place 1 queen:

N Queens Example 2

Clarify the problem

What are some questions you'd ask an interviewer?

Understand the problem

How many solutions should we return when n = 2?
0
1
2
4

Remember to understand the problem and plan your approach before you start coding.