We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 0e05147 + 6b12969 commit f7fa587Copy full SHA for f7fa587
suyeun84/202509/16 PGM LV2 N-Queen.md
@@ -0,0 +1,35 @@
1
+```java
2
+class Solution {
3
+ private static int[] board;
4
+ private static int answer;
5
+
6
+ public static int solution(int n) {
7
+ board = new int[n];
8
9
+ backTracking(0, n);
10
11
+ return answer;
12
+ }
13
14
+ private static void backTracking(int depth, int n) {
15
+ if (depth == n) {
16
+ answer++;
17
+ return;
18
19
+ for (int i = 0; i < n; i++) {
20
+ board[depth] = i;
21
+ if (valid(depth)) {
22
+ backTracking(depth + 1, n);
23
24
25
26
27
+ private static boolean valid(int i) {
28
+ for (int j = 0; j < i; j++) {
29
+ if (board[i] == board[j]) return false;
30
+ if (Math.abs(i - j) == Math.abs(board[i] - board[j])) return false;
31
32
+ return true;
33
34
+}
35
+```
0 commit comments