不同路径1、2、最小路径和
1.1 解法一:递归(时间超限制)
public int uniquePaths(int m, int n) {
return solve1(m-1, n-1);
}
private int solve1(int x, int y) {
if (x==0 || y==0) return 1;
return solve1(x, y-1) + solve1(x-1, y);
}
1.2 解法二:递归+记忆化搜索
public int uniquePaths(int m, int n) {
int[][] a = new int[m][n];
return solve2(m-1, n-1, a);
}
private int solve2(int x, int y, int[][] a) {
if (x==0 || y==0) return 1;
if (a[x][y]!=0) return a[x][y];
a[x][y] = solve2(x-1, y, a) + solve2(x, y-1, a);
return a[x][y];
}
1.3 解法三:DP,动态递推
(1)DP:使用二维数组存储中间状态
public int uniquePaths(int m, int n) {
int[][] dp = new int[m][n];
for (int i=0; i<m; i++) {
for (int j=0; j<n; j++) {
if (i==0 || j==0) dp[i][j] = 1;
else dp[i][j] = dp[i-1][j] + dp[i][j-1];
}
}
return dp[m-1][n-1];
}
(2)DP:使用一维数组存储中间状态
public int uniquePaths(int m, int n) {
int[] dp = new int[n];
Arrays.fill(dp, 1);
for (int i=1; i<m; i++) {
for (int j=1; j<n; j++) {
dp[j] = dp[j-1] + dp[j];
}
}
return dp[n-1];
}
2.1 方案一:递归(时间超限制)
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int m = obstacleGrid.length, n=obstacleGrid[0].length;
return solve1(m-1, n-1, obstacleGrid);
}
private int solve1(int x, int y, int[][] obstacleGrid) {
if (obstacleGrid[x][y]==1) return 0;
if (x==0 && y==0) return 1;
if (x==0) return solve1(x, y-1, obstacleGrid);
if (y==0) return solve1(x-1, y, obstacleGrid);
return solve1(x-1, y, obstacleGrid) + solve1(x, y-1, obstacleGrid);
}
2.2 方案二:递归+记忆化搜索
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int m = obstacleGrid.length, n = obstacleGrid[0].length;
int[][] a = new int[m][n];
for (int i=0; i<m; i++) {
a[i] = new int[n];
Arrays.fill(a[i], -1);
}
return solve2(m-1, n-1, obstacleGrid, a);
}
private int solve1(int x, int y, int[][] obstacleGrid) {
if (obstacleGrid[x][y]==1) return 0;
if (x==0 && y==0) return 1;
if (x==0) return solve1(x, y-1, obstacleGrid);
if (y==0) return solve1(x-1, y, obstacleGrid);
return solve1(x-1, y, obstacleGrid) + solve1(x, y-1, obstacleGrid);
}
2.3 方案三:DP:动态递推
(1)动态递推:使用二维数组
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int m = obstacleGrid.length, n = obstacleGrid[0].length;
int[][] dp = new int[m][n];
for (int i=0; i<m; i++) {
for (int j=0; j<n; j++) {
if (obstacleGrid[i][j]==1) dp[i][j] = 0;
else if (i==0 && j==0) dp[i][j] = 1;
else if (i==0) dp[i][j] = dp[i][j-1];
else if (j==0) dp[i][j] = dp[i-1][j];
else dp[i][j] = dp[i-1][j] + dp[i][j-1];
}
}
return dp[m-1][n-1];
}
(2)动态递推:使用一维数组
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
// 方案四: DP动态递推:使用一维数组
int m = obstacleGrid.length, n = obstacleGrid[0].length;
int[] dp = new int[n];
for (int i=0; i<n; i++) {
if (obstacleGrid[0][i]==1) break;
else dp[i]=1;
}
for (int i=1; i<m; i++) {
for (int j=0; j<n; j++) {
if (obstacleGrid[i][j]==1) dp[j]=0;
else if (j>0) {
dp[j] = dp[j] + dp[j-1];
}
}
}
return dp[n-1];
}
3.1 方案一:递归:时间超限制
public int minPathSum(int[][] grid) {
int m = grid.length, n = grid[0].length;
return solve1(m-1, n-1, grid);
}
private int solve1(int x, int y, int[][] grid) {
if (x==0 && y==0) return grid[x][y];
else if (x==0) return grid[x][y] + solve1(x, y-1, grid);
else if (y==0) return grid[x][y] + solve1(x-1, y, grid);
else return grid[x][y] + Math.min(solve1(x, y-1, grid), solve1(x-1, y, grid));
}
3.2 方案二:递归+记忆化搜索
public int minPathSum(int[][] grid) {
int m = grid.length, n = grid[0].length;
int[][] a = new int[m][n];
for (int i=0; i<m; i++) {
a[i] = new int[n];
Arrays.fill(a[i], -1);
}
return solve2(m-1, n-1, grid, a);
}
private int solve2(int x, int y, int[][] grid, int[][] a) {
if (a[x][y]!=-1) return a[x][y];
if (x==0 && y==0) {
a[x][y] = grid[x][y];
}else if (x==0) {
a[x][y] = grid[x][y] + solve2(x, y-1, grid, a);
}else if (y==0) {
a[x][y] = grid[x][y] + solve2(x-1, y, grid, a);
}else {
a[x][y] = grid[x][y] + Math.min(solve2(x, y-1, grid, a), solve2(x-1, y, grid, a));
}
return a[x][y];
}
3.3 方案三:DP:动态规划
(1)动态递推:使用二维数组
public int minPathSum(int[][] grid) {
int m = grid.length, n = grid[0].length;
int[][] dp = new int[m][n];
for (int i=0; i<m; i++) {
for (int j=0; j<n; j++) {
if (i==0 && j==0) dp[i][j] = grid[i][j];
else if (i==0) dp[i][j] = grid[i][j] + dp[i][j-1];
else if (j==0) dp[i][j] = grid[i][j] + dp[i-1][j];
else dp[i][j] = grid[i][j] + Math.min(dp[i-1][j] , dp[i][j-1]);
}
}
return dp[m-1][n-1];
}
(2)动态递推:使用一维数组
public int minPathSum(int[][] grid) {
int m = grid.length, n = grid[0].length;
int[] dp = new int[n];
dp[0] = grid[0][0];
for (int i=1; i<n; i++) {
dp[i]=grid[0][i] + dp[i-1];
}
for (int i=1; i<m; i++) {
for (int j=0; j<n; j++) {
if (j==0) dp[j] = grid[i][j] + dp[j];
else dp[j] = grid[i][j] + Math.min(dp[j], dp[j-1]);
}
}
return dp[n-1];
}