-
Notifications
You must be signed in to change notification settings - Fork 44
/
Problem38.js
63 lines (58 loc) · 1.76 KB
/
Problem38.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Problem 38
//
// This problem was asked by Microsoft.
//
// You have an N by N board. Write a function that, given N, returns the number of possible arrangements of the board where N queens can be placed on the board without threatening each other.
// i.e. no two queens share the same row, column, or diagonal.
//
// https://leetcode.com/problems/n-queens-ii/description/
//
// O(N!) Time complexity
// O(N) Space complexity
// N is the number of queens
/**
* Returns the number of possible arrangements of the board where N queens can be placed on the board without threatening each other.
* @param {number} n
* @return {number}
*/
function totalNQueens(n) {
// col represents the columns that are taken
// tlbr represents the \ diagonals that are taken
// trbl represents the / diagonals that are taken
const col = Array(n).fill(false);
const tlbr = Array(2 * n + 1).fill(false); // top left -> bottom right
const trbl = Array(2 * n + 1).fill(false); // top right -> bottom left
return helper(0, n, col, tlbr, trbl);
}
/**
* Recursive helper function
* @param {number} row
* @param {number} n
* @param {number} col
* @param {boolean[]} tlbr
* @param {boolean[]} trbl
* @return {number}
*/
function helper(row, n, col, tlbr, trbl) {
let result = 0;
for (let i = 0; i < n; i++) {
if (!col[i] && !tlbr[n - 1 - row + i] && !trbl[row + i]) {
if (row === n - 1) {
result++;
} else {
// choose
col[i] = true;
tlbr[n - 1 - row + i] = true;
trbl[row + i] = true;
// explore
result += helper(row + 1, n, col, tlbr, trbl);
// unchoose
col[i] = false;
tlbr[n - 1 - row + i] = false;
trbl[row + i] = false;
}
}
}
return result;
}
export default totalNQueens;