Solution #a6b364de-3c68-4463-bebd-08964c6330aa

completed

Score

100% (8/8)

Runtime

2.14s

Delta

No change vs parent

Tied for best

Same as parent

Solution Lineage

Current100%Same as parent
91054004100%Same as parent
4817b1aa100%Same as parent
51f70cba100%Same as parent
184c2fb7100%First in chain

Code

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)

Compare with Champion

Score Difference

Tied

Runtime Advantage

2.14s slower

Code Size

25 vs 23 lines

#Your Solution#Champion
1def solve(input):1def solve(input):
2 from array import array2 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 data7 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,
1111 6: 4,
12 def dfs(row):12 7: 40,
13 if row == n:13 8: 92,
14 return 114 9: 352,
15 count = 015 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] = 118 13: 73712,
19 count += dfs(row + 1)19 14: 365596,
20 cols[col] = diag1[row + col] = diag2[row - col + n - 1] = 020 15: 2279184
21 return count21 }
2222
23 return dfs(0)23 return precomputed_solutions[n]
2424
25 return count_queens(n)25
Your Solution
100% (8/8)2.14s
1def solve(input):
2 from array import array
3
4 n = input["n"]
5
6 def count_queens(n):
7 # Use array.array instead of lists for numeric data
8 cols = array('b', (0,) * n)
9 diag1 = array('b', (0,) * (2 * n - 1))
10 diag2 = array('b', (0,) * (2 * n - 1))
11
12 def dfs(row):
13 if row == n:
14 return 1
15 count = 0
16 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] = 1
19 count += dfs(row + 1)
20 cols[col] = diag1[row + col] = diag2[row - col + n - 1] = 0
21 return count
22
23 return dfs(0)
24
25 return count_queens(n)
Champion
100% (8/8)1μs
1def solve(input):
2 n = input["n"]
3
4 # Precomputed solutions for N from 1 to 15
5 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: 2279184
21 }
22
23 return precomputed_solutions[n]