forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathN-Queens II.js
43 lines (38 loc) · 1.29 KB
/
N-Queens II.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* @param {number} n
* @return {number}
*/
var totalNQueens = function(n) {
// Keep track of columns with queens
const cols = new Set();
// Keep track of positive slope diagonal by storing row number + column number
const posDiag = new Set();
// Keep track of negative slope diagonal by storing row number - column number
const negDiag = new Set();
// Count of valid boards
let count = 0;
const backtrack = function(r) {
// Base case to end recursion, we have traversed board and found valid position in each row
if(r === n) {
count += 1;
return;
}
// Loop through each column to see if you can place a queen
for(let c = 0; c < n; c++) {
// invalid position check, if position in set we cannot place queen here
if(cols.has(c) || posDiag.has(r+c) || negDiag.has(r-c)) continue;
// add new queen to sets
cols.add(c);
posDiag.add(r+c);
negDiag.add(r-c);
// backtrack
backtrack(r+1);
// remove current position from sets because backtracking for this position is complete
cols.delete(c);
posDiag.delete(r+c);
negDiag.delete(r-c);
}
}
backtrack(0);
return count;
};