Skip to content

Commit

Permalink
Create 0052-n-queens-ii.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
a93a authored Feb 2, 2023
1 parent f21637c commit 4445cdc
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions kotlin/0052-n-queens-ii.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Solution {
fun totalNQueens(n: Int): Int {

val cols = HashSet<Int>()
val pD = HashSet<Int>()
val nD = HashSet<Int>()
var res = 0

fun fill(row: Int) {

if(row == n) {
res++
return
}

for(col in 0 until n) {
if(cols.contains(col) || pD.contains(row + col) || nD.contains(row - col)) continue

cols.add(col)
pD.add(row + col)
nD.add(row - col)

fill(row+1)

cols.remove(col)
pD.remove(row + col)
nD.remove(row - col)
}
}

fill(0)
return res
}
}

0 comments on commit 4445cdc

Please sign in to comment.