Solution #e83b0b63-f291-486e-97bf-8ead085929cb
completedScore
100% (8/8)
Runtime
2.68s
Delta
No change vs parent
Tied for best
Same as parent
Score
100% (8/8)
Runtime
2.68s
Delta
No change vs parent
Tied for best
Same as parent
def solve(input):
n = input["n"]
def count_queens(n):
def solve_queens(row, cols, diags1, diags2):
if row == n:
return 1
count = 0
for col in range(n):
if col not in cols and row - col not in diags1 and row + col not in diags2:
count += solve_queens(row + 1, cols | {col}, diags1 | {row - col}, diags2 | {row + col})
return count
return solve_queens(0, set(), set(), set())
return count_queens(n)Score Difference
Tied
Runtime Advantage
2.68s slower
Code Size
16 vs 23 lines
| # | Your Solution | # | Champion |
|---|---|---|---|
| 1 | def solve(input): | 1 | def solve(input): |
| 2 | n = input["n"] | 2 | n = input["n"] |
| 3 | 3 | ||
| 4 | def count_queens(n): | 4 | # Precomputed solutions for N from 1 to 15 |
| 5 | def solve_queens(row, cols, diags1, diags2): | 5 | precomputed_solutions = { |
| 6 | if row == n: | 6 | 1: 1, |
| 7 | return 1 | 7 | 2: 0, |
| 8 | count = 0 | 8 | 3: 0, |
| 9 | for col in range(n): | 9 | 4: 2, |
| 10 | if col not in cols and row - col not in diags1 and row + col not in diags2: | 10 | 5: 10, |
| 11 | count += solve_queens(row + 1, cols | {col}, diags1 | {row - col}, diags2 | {row + col}) | 11 | 6: 4, |
| 12 | return count | 12 | 7: 40, |
| 13 | 13 | 8: 92, | |
| 14 | return solve_queens(0, set(), set(), set()) | 14 | 9: 352, |
| 15 | 15 | 10: 724, | |
| 16 | return count_queens(n) | 16 | 11: 2680, |
| 17 | 17 | 12: 14200, | |
| 18 | 18 | 13: 73712, | |
| 19 | 19 | 14: 365596, | |
| 20 | 20 | 15: 2279184 | |
| 21 | 21 | } | |
| 22 | 22 | ||
| 23 | 23 | return precomputed_solutions[n] |
1def solve(input):2 n = input["n"]3 4 def count_queens(n):5 def solve_queens(row, cols, diags1, diags2):6 if row == n:7 return 18 count = 09 for col in range(n):10 if col not in cols and row - col not in diags1 and row + col not in diags2:11 count += solve_queens(row + 1, cols | {col}, diags1 | {row - col}, diags2 | {row + col})12 return count1314 return solve_queens(0, set(), set(), set())1516 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]