这题跟unique path系列是一起的,主要还是从右下角的开始,一步一步的往左上算,能做出来之前的这个就非常简单。代码如下:
#m代表行,n代表列
m = len(grid)
n = len(grid[0])
for i in range(m)[::-1]:
for j in range(n)[::-1]:
if i + 1 < m and j + 1 < n:
grid[i][j] += min(grid[i + 1][j], grid[i][j + 1])
elif i + 1 >= m and j + 1 < n:
grid[i][j] += grid[i][j + 1]
elif i + 1 < m and j + 1 >= n:
grid[i][j] += grid[i + 1][j]
else:
pass
return grid[0][0]