https://leetcode.com/problems/unique-paths/
1. recursion:
class Solution(object):def uniquePaths(self, m, n):""":type m: int:type n: int:rtype: int"""if m==1 or n==1:return 1return self.uniquePaths(m - 1, n) + self.uniquePaths(m, n - 1)
2. dynamic programming:
class Solution(object):def uniquePaths(self, m, n):""":type m: int:type n: int:rtype: int"""# set up a m*n matrix, all values are 1d = [([1] * n) for _ in range(m)]# The number in the matrix represents# the number of paths that could reach the current one for top-left## Also, the one instead of in the 1st row or 1st column are sum of its left one and its upper one## for example, m = 3, n = 4# d = 1 1 1 1# 1 1 1 1# 1 1 1 1# --># d = 1 1 1 1# 1 2 3 4# 1 3 6 10for col in range(1, m):for row in range(1, n):d[col][row] = d[col - 1][row] + d[col][row - 1]return d[m - 1][n - 1]
