799. N-Queens II — Count All Solutions (Optimized)

HardRecursionRecursion

Given n and c, count N-Queens solutions on an n×n board with the queen in row 0 fixed at column c. Use optimized backtracking with bitmask pruning. Return 0 if c >= n.

Input: Two integers n and c.

Output: Return solution count.

Examples

Example 1
Input: 8, 0
Output: 3
Explanation: 3 solutions with Q at (0,0) on 8×8.
Example 2
Input: 4, 1
Output: 1
Explanation: One solution.
Example 3
Input: 1, 0
Output: 1
Explanation: Trivial.

Constraints

Asked by

AmazonGoogleMicrosoftMetaAdobe
Solve this problem in the editor →