Easy python solution beats 99%


  • 0
    T

    '''

    Here is the solution:

    def findpattern(self, x, table, current, m, n):
    
        table[x] = True
    
        if current >= m:
            self.result += 1
        
        for i in range(1,10):
            if table[i]:
                continue
            if not table[self.able[x][i]]:
                continue
            
            if current == n - 1:
                self.result += 1
            else:
                self.findpattern(i, table, current + 1, m, n)
        
        table[x] = False
            
    
    def numberOfPatterns(self, m, n):
        """
        :type m: int
        :type n: int
        :rtype: int
        """
        if n == 1:
            return 9
        
        self.result = 0
        
        self.able = [[0 for j in range(10)] for i in range(10)]
        self.able[1][3] = self.able[3][1] = 2
        self.able[1][7] = self.able[7][1] = 4
        self.able[9][3] = self.able[3][9] = 6
        self.able[7][9] = self.able[9][7] = 8
        self.able[1][9] = self.able[9][1] = 5
        self.able[7][3] = self.able[3][7] = 5
        self.able[2][8] = self.able[8][2] = 5
        self.able[4][6] = self.able[6][4] = 5
        
        
        table = [False for i in range(10)]
        table[0] = True
        self.findpattern(1, table, 1, m, n)
        self.findpattern(2, table, 1, m, n)
        
        self.result *= 4
        
        self.findpattern(5, table, 1, m, n)
        
        return self.result
    

    '''


Log in to reply
 

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