forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
n-queens-ii.py
38 lines (33 loc) · 1.32 KB
/
n-queens-ii.py
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
from functools import reduce
# Time: O(n!)
# Space: O(n)
class Solution(object):
# @return an integer
def totalNQueens(self, n):
self.cols = [False] * n
self.main_diag = [False] * (2 * n)
self.anti_diag = [False] * (2 * n)
return self.totalNQueensRecu([], 0, n)
def totalNQueensRecu(self, solution, row, n):
if row == n:
return 1
result = 0
for i in xrange(n):
if not self.cols[i] and not self.main_diag[row + i] and not self.anti_diag[row - i + n]:
self.cols[i] = self.main_diag[row + i] = self.anti_diag[row - i + n] = True
result += self.totalNQueensRecu(solution + [i], row + 1, n)
self.cols[i] = self.main_diag[row + i] = self.anti_diag[row - i + n] = False
return result
# slower solution
class Solution2(object):
# @return an integer
def totalNQueens(self, n):
return self.totalNQueensRecu([], 0, n)
def totalNQueensRecu(self, solution, row, n):
if row == n:
return 1
result = 0
for i in xrange(n):
if i not in solution and reduce(lambda acc, j: abs(row - j) != abs(i - solution[j]) and acc, xrange(len(solution)), True):
result += self.totalNQueensRecu(solution + [i], row + 1, n)
return result