Simplest Python code


  • 0
    B

    Got TLE for n = 9. Locally it takes 1.03 s to run.

    class Solution:
        # @return an integer
        def totalNQueens(self, n):
            solutions = []
            for solution in itertools.permutations(range(n)):
                if len(set(x-y for x,y in enumerate(solution))) == \
                   len(set(x+y for x,y in enumerate(solution))) == n:
                    solutions.append(solution)
            return len(solutions)

  • 0
    J

    must import itertools to run ?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.