My python dfs solution 80ms AC


  • 0
    L
    class Solution(object):
        def __dfs(self,arg):
            if arg[4]==arg[5]:
                arg[0]+=1
            else:
                for i in range(arg[5]):
                    if not(arg[1][i] or arg[2][i+arg[4]] or arg[3][i-arg[4]+arg[5]-1]):
                        arg[1][i]=True
                        arg[2][i+arg[4]]=True
                        arg[3][i-arg[4]+arg[5]-1]=True
                        arg[4]+=1
                        self.__dfs(arg)
                        arg[4]-=1
                        arg[1][i]=False
                        arg[2][i+arg[4]]=False
                        arg[3][i-arg[4]+arg[5]-1]=False
        def totalNQueens(self, n):
            arg=[0,[False]*n,[False]*(2*n-1),[False]*(2*n-1),0,n]
            self.__dfs(arg)
            return arg[0]

Log in to reply
 

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