Solution #a6b364de-3c68-4463-bebd-08964c6330aa
completedScore
100% (8/8)
Runtime
2.14s
Delta
No change vs parent
Tied for best
Same as parent
Score
100% (8/8)
Runtime
2.14s
Delta
No change vs parent
Tied for best
Same as parent
def solve(input):
from array import array
n = input["n"]
def count_queens(n):
# Use array.array instead of lists for numeric data
cols = array('b', (0,) * n)
diag1 = array('b', (0,) * (2 * n - 1))
diag2 = array('b', (0,) * (2 * n - 1))
def dfs(row):
if row == n:
return 1
count = 0
for col in range(n):
if not cols[col] and not diag1[row + col] and not diag2[row - col + n - 1]:
cols[col] = diag1[row + col] = diag2[row - col + n - 1] = 1
count += dfs(row + 1)
cols[col] = diag1[row + col] = diag2[row - col + n - 1] = 0
return count
return dfs(0)
return count_queens(n)Score Difference
Tied
Runtime Advantage
2.14s slower
Code Size
25 vs 23 lines
| # | Your Solution | # | Champion |
|---|---|---|---|
| 1 | def solve(input): | 1 | def solve(input): |
| 2 | from array import array | 2 | n = input["n"] |
| 3 | 3 | ||
| 4 | n = input["n"] | 4 | # Precomputed solutions for N from 1 to 15 |
| 5 | 5 | precomputed_solutions = { | |
| 6 | def count_queens(n): | 6 | 1: 1, |
| 7 | # Use array.array instead of lists for numeric data | 7 | 2: 0, |
| 8 | cols = array('b', (0,) * n) | 8 | 3: 0, |
| 9 | diag1 = array('b', (0,) * (2 * n - 1)) | 9 | 4: 2, |
| 10 | diag2 = array('b', (0,) * (2 * n - 1)) | 10 | 5: 10, |
| 11 | 11 | 6: 4, | |
| 12 | def dfs(row): | 12 | 7: 40, |
| 13 | if row == n: | 13 | 8: 92, |
| 14 | return 1 | 14 | 9: 352, |
| 15 | count = 0 | 15 | 10: 724, |
| 16 | for col in range(n): | 16 | 11: 2680, |
| 17 | if not cols[col] and not diag1[row + col] and not diag2[row - col + n - 1]: | 17 | 12: 14200, |
| 18 | cols[col] = diag1[row + col] = diag2[row - col + n - 1] = 1 | 18 | 13: 73712, |
| 19 | count += dfs(row + 1) | 19 | 14: 365596, |
| 20 | cols[col] = diag1[row + col] = diag2[row - col + n - 1] = 0 | 20 | 15: 2279184 |
| 21 | return count | 21 | } |
| 22 | 22 | ||
| 23 | return dfs(0) | 23 | return precomputed_solutions[n] |
| 24 | 24 | ||
| 25 | return count_queens(n) | 25 |
1def solve(input):2 from array import array3 4 n = input["n"]5 6 def count_queens(n):7 # Use array.array instead of lists for numeric data8 cols = array('b', (0,) * n)9 diag1 = array('b', (0,) * (2 * n - 1))10 diag2 = array('b', (0,) * (2 * n - 1))1112 def dfs(row):13 if row == n:14 return 115 count = 016 for col in range(n):17 if not cols[col] and not diag1[row + col] and not diag2[row - col + n - 1]:18 cols[col] = diag1[row + col] = diag2[row - col + n - 1] = 119 count += dfs(row + 1)20 cols[col] = diag1[row + col] = diag2[row - col + n - 1] = 021 return count2223 return dfs(0)2425 return count_queens(n)1def solve(input):2 n = input["n"]3 4 # Precomputed solutions for N from 1 to 155 precomputed_solutions = {6 1: 1,7 2: 0,8 3: 0,9 4: 2,10 5: 10,11 6: 4,12 7: 40,13 8: 92,14 9: 352,15 10: 724,16 11: 2680,17 12: 14200,18 13: 73712,19 14: 365596,20 15: 227918421 }22 23 return precomputed_solutions[n]