Python 1 line solution


  • 1
    def findPaths(self, m, n, N, x, y):
            M = [[0 for i in range(n)] for j in range(m)]
            for _ in range(N):
                M = [[(i == 0 or M[i - 1][j]) + (i + 1 == m or M[i + 1][j])
                      + (j == 0 or M[i][j - 1]) + (j + 1 == n or M[i][j + 1])
                      for j in range(n)] for i in range(m)]
            return M[x][y] % (10 ** 9 + 7)
    

    Make it 1-line:

    def findPaths(self, m, n, N, x, y):
            return reduce(lambda M, _:
                  [[(i == 0 or M[i - 1][j]) + (i + 1 == m or M[i + 1][j])
                  + (j == 0 or M[i][j - 1]) + (j + 1 == n or M[i][j + 1])
                  for j in range(n)] for i in range(m)], range(N),
                  [[0 for i in range(n)] for j in range(m)])[x][y] % (10 ** 9 + 7)

Log in to reply
 

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