hardBacktrackingBacktracking

N-Queens II

hardTime: O(n!)Space: O(n)

Signals to notice

count N-Queens solutionssame as N-Queens I but count onlyno board construction needed

Brute force first

Same backtracking as N-Queens I.

The key insight

Same backtracking but increment counter instead of building boards. Slightly faster — no string construction. O(n!).

What must stay true

Same constraint checking: columns, positive diag, negative diag. Just count valid complete placements.

Easy way to go wrong

Building board strings — unnecessary for counting. Just count.

Backtracking Pattern