Solution #91054004-8a38-43d6-bdce-23480f016355

completed

Score

100% (8/8)

Runtime

3.34s

Delta

No change vs parent

Tied for best

Same as parent

Solution Lineage

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

Code

def solve(input):
    n = input["n"]
    
    # Using a bisect-based approach to optimize N-Queens intermediate states
    def count_queens(n):
        def dfs(queens, xy_dif, xy_sum):
            p = len(queens)
            if p == n:
                return 1
            count = 0
            for q in range(n):
                if q not in queens and p - q not in xy_dif and p + q not in xy_sum:
                    count += dfs(queens + [q], xy_dif + [p - q], xy_sum + [p + q])
            return count

        return dfs([], [], [])

    # A bisect-based approach can be explored in future iterations to precompute
    # a list or cache of solutions for fast lookup, yet due to the problem scope (1-15),
    # the computation cost within these bounds is manageable.
    return count_queens(n)

Compare with Champion

Score Difference

Tied

Runtime Advantage

3.34s slower

Code Size

21 vs 23 lines

#Your Solution#Champion
1def solve(input):1def solve(input):
2 n = input["n"]2 n = input["n"]
3 3
4 # Using a bisect-based approach to optimize N-Queens intermediate states4 # Precomputed solutions for N from 1 to 15
5 def count_queens(n):5 precomputed_solutions = {
6 def dfs(queens, xy_dif, xy_sum):6 1: 1,
7 p = len(queens)7 2: 0,
8 if p == n:8 3: 0,
9 return 19 4: 2,
10 count = 010 5: 10,
11 for q in range(n):11 6: 4,
12 if q not in queens and p - q not in xy_dif and p + q not in xy_sum:12 7: 40,
13 count += dfs(queens + [q], xy_dif + [p - q], xy_sum + [p + q])13 8: 92,
14 return count14 9: 352,
1515 10: 724,
16 return dfs([], [], [])16 11: 2680,
1717 12: 14200,
18 # A bisect-based approach can be explored in future iterations to precompute18 13: 73712,
19 # a list or cache of solutions for fast lookup, yet due to the problem scope (1-15),19 14: 365596,
20 # the computation cost within these bounds is manageable.20 15: 2279184
21 return count_queens(n)21 }
2222
2323 return precomputed_solutions[n]
Your Solution
100% (8/8)3.34s
1def solve(input):
2 n = input["n"]
3
4 # Using a bisect-based approach to optimize N-Queens intermediate states
5 def count_queens(n):
6 def dfs(queens, xy_dif, xy_sum):
7 p = len(queens)
8 if p == n:
9 return 1
10 count = 0
11 for q in range(n):
12 if q not in queens and p - q not in xy_dif and p + q not in xy_sum:
13 count += dfs(queens + [q], xy_dif + [p - q], xy_sum + [p + q])
14 return count
15
16 return dfs([], [], [])
17
18 # A bisect-based approach can be explored in future iterations to precompute
19 # a list or cache of solutions for fast lookup, yet due to the problem scope (1-15),
20 # the computation cost within these bounds is manageable.
21 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]