/52. N-Queens II

52. N-Queens II

Hard
Backtracking78.3% acceptance

Given an integer board_size, compute the number of distinct ways to place board_size queens on a board_size x board_size grid such that no two queens are in the same row, column, or diagonal.

Example 1

Input: 5

Output: 10

Explanation: There are 10 distinct ways to place 5 queens on a 5x5 board without conflicts.

Example 2

Input: 2

Output: 0

Explanation: No valid arrangement exists for 2 queens on a 2x2 board.

Constraints

  • 1 <= board_size <= 9
  • Input is a single integer board_size
Python (current runtime)

Case 1

Input: 3

Expected: 0

Case 2

Input: 6

Expected: 4

Case 3

Input: 7

Expected: 40