Skip to content

Commit ab60371

Browse files
solves #52: n queens ii in java
1 parent cb364a5 commit ab60371

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
| 49 | [Group Anagrams](https://leetcode.com/problems/group-anagrams) | [![Java](assets/java.png)](src/GroupAnagrams.java) | |
5959
| 50 | [Pow(x,n)](https://leetcode.com/problems/powx-n) | [![Java](assets/java.png)](src/Powxn.java) | |
6060
| 51 | [N-Queens](https://leetcode.com/problems/n-queens) | [![Java](assets/java.png)](src/NQueens.java) | |
61+
| 51 | [N-Queens II](https://leetcode.com/problems/n-queens-ii) | [![Java](assets/java.png)](src/NQueensII.java) | |
6162
| 53 | [Maximum SubArray](https://leetcode.com/problems/maximum-subarray) | [![Java](assets/java.png)](src/MaximumSubArray.java) [![Python](assets/python.png)](python/maximum_sum_subarray.py) | |
6263
| 54 | [Spiral Matrix](https://leetcode.com/problems/spiral-matrix) | [![Java](assets/java.png)](src/SpiralMatrix.java) | |
6364
| 55 | [Jump Game](https://leetcode.com/problems/jump-game) | [![Java](assets/java.png)](src/JumpGame.java) | |

src/NQueensII.java

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// https://leetcode.com/problems/n-queens-ii
2+
// T: O(N!)
3+
// S: O(N^2)
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class NQueensII {
9+
private static boolean[] rows, columns;
10+
private static int result = 0;
11+
12+
public int totalNQueens(int n) {
13+
result = 0;
14+
final List<String> board = getEmptyBoard(n);
15+
rows = new boolean[n];
16+
columns = new boolean[n];
17+
nQueens(0, n, board, 0);
18+
return result;
19+
}
20+
21+
private static void nQueens(int row, int n, List<String> board, int queens) {
22+
if (row == n) {
23+
if (queens == n) {
24+
result++;
25+
}
26+
return;
27+
}
28+
29+
for (int column = 0 ; column < n ; column++) {
30+
if (canPlace(board, row, column)) {
31+
placeQueen(board, row, column);
32+
nQueens(row + 1, n, board, queens + 1);
33+
removeQueen(board, row, column);
34+
}
35+
}
36+
}
37+
38+
private static void placeQueen(List<String> board, int row, int column) {
39+
board.set(
40+
row,
41+
board.get(row).substring(0, column) + 'Q' + board.get(row).substring(column + 1)
42+
);
43+
rows[row] = true;
44+
columns[column] = true;
45+
}
46+
47+
private static void removeQueen(List<String> board, int row, int column) {
48+
board.set(
49+
row,
50+
board.get(row).substring(0, column) + '.' + board.get(row).substring(column + 1)
51+
);
52+
rows[row] = false;
53+
columns[column] = false;
54+
}
55+
56+
private static boolean canPlace(List<String> board, int row, int column) {
57+
return !rows[row] && !columns[column] && !queenInLeftDiagonal(board, row, column)
58+
&& !queenInRightDiagonal(board, row, column);
59+
}
60+
61+
private static boolean queenInLeftDiagonal(List<String> board, int row, int column) {
62+
for (int i = row - 1, j = column - 1 ; i >= 0 && j >= 0 ; i--, j--) {
63+
if (board.get(i).charAt(j) == 'Q') {
64+
return true;
65+
}
66+
}
67+
for (int i = row + 1, j = column + 1 ; i < board.size() && j < board.size() ; i++, j++) {
68+
if (board.get(i).charAt(j) == 'Q') {
69+
return true;
70+
}
71+
}
72+
return false;
73+
}
74+
75+
private static boolean queenInRightDiagonal(List<String> board, int row, int column) {
76+
for (int i = row - 1, j = column + 1 ; i >= 0 && j < board.size() ; i--, j++) {
77+
if (board.get(i).charAt(j) == 'Q') {
78+
return true;
79+
}
80+
}
81+
for (int i = row + 1, j = column - 1 ; i < board.size() && j >= 0 ; i++, j--) {
82+
if (board.get(i).charAt(j) == 'Q') {
83+
return true;
84+
}
85+
}
86+
return false;
87+
}
88+
89+
private static List<String> getEmptyBoard(int n) {
90+
final List<String> board = new ArrayList<>();
91+
final String line = ".".repeat(n);
92+
for (int i = 0 ; i < n ; i++) {
93+
board.add(line);
94+
}
95+
return board;
96+
}
97+
}

0 commit comments

Comments
 (0)