62.不同路径
62.不同路径
题解
state: dp[x][y]从起点走到 x,y 的路径
function: dp[x][y] = dp[x-1][y], dp[x][y-1]
intialize: 第一行和第一列皆为1
answer: dp[n-1][m-1]
代码
package main func min(a, b int) int { if a > b { return b } return a } func uniquePaths(m int, n int) int { dp := make([][]int, m) for i := 0; i < m; i++ { dp[i] = make([]int, n) dp[i][0] = 1 } for i := 0; i < n; i++ { dp[0][i] = 1 } for i := 1; i < m; i++ { for j := 1; j < n; j++ { dp[i][j] = dp[i-1][j] + dp[i][j-1] } } return dp[m-1][n-1] }