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?
思路:给一个方格,起点在左上角,终点在右下角。利用动态规划每一步等于左边的步数加上上一格的步数。
class Solution(object):
def uniquePaths(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
"""
flag = [[0 for j in range(n)] for i in range(m)]
flag[0][0] = 1
for j in range(n):
for i in range(m):
if i!=0 or j!=0:
if i-1<0:top = 0
else:top=flag[i-1][j]
if j-1<0:left = 0
else:
left=flag[i][j-1]
flag[i][j] = top+left
return flag[m-1][n-1]