-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathNQueensII.java
More file actions
49 lines (44 loc) · 1.24 KB
/
NQueensII.java
File metadata and controls
49 lines (44 loc) · 1.24 KB
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
package algorithm.lc;
import java.util.ArrayList;
/**
* Follow up for N-Queens problem.
*
* Now, instead outputting board configurations, return the total number of
* distinct solutions.
*
*/
public class NQueensII {
// O(n) space, O(n!) time
public class Solution {
public int totalNQueens(int n) {
// Start typing your Java solution below
// DO NOT write main() function
ArrayList<Integer> res = new ArrayList<Integer>();
res.add(0);
int[] history = new int[n];
int curRow = 0;
solve(history, n, curRow, res);
return res.get(0);
}
private void solve(int[] history, int n, int curRow, ArrayList<Integer> res) {
if (curRow == n) { // add to res
res.set(0, res.get(0) + 1);
} else {
for (int c = 0; c < n; ++c) { // try position curIdx, c
boolean canPut = true;
for (int r = 0; r < curRow; ++r) {
if (history[r] == c || c - curRow == history[r] - r // diagonal
|| c + curRow == history[r] + r) {
canPut = false;
break;
}
}
if (canPut) {
history[curRow] = c;
solve(history, n, curRow + 1, res);
}
}
}
}
}
}