A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
How many possible unique paths are there?
Above is a 3 x 7 grid. How many possible unique paths are there?
Note: m and n will be at most 100.
思路:类似于这种动态规划的题,要注意第一行和第一列的区别。
class Solution(object):
def uniquePaths(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
"""
res = [[1 for _ in range(n)] for _ in range(m)]
for i in range(1,m):
for j in range(1,n):
res[i][j] = res[i-1][j]+res[i][j-1]
return res[-1][-1]