第九章 动态规划part02
● 62.不同路径
● 63. 不同路径 II
今天开始逐渐有 dp的感觉了,题目不多,就两个 不同路径,可以好好研究一下
详细布置
62.不同路径
本题大家掌握动态规划的方法就可以。 数论方法 有点非主流,很难想到。
https://programmercarl.com/0062.%E4%B8%8D%E5%90%8C%E8%B7%AF%E5%BE%84.html
视频讲解:https://www.bilibili.com/video/BV1ve4y1x7Eu
63. 不同路径 II
https://programmercarl.com/0063.%E4%B8%8D%E5%90%8C%E8%B7%AF%E5%BE%84II.htmlhttps://programmercarl.com/0063.%E4%B8%8D%E5%90%8C%E8%B7%AF%E5%BE%84II.html
视频讲解:https://www.bilibili.com/video/BV1Ld4y1k7c6
目录
0062_不同路径
package com.question.solve.leetcode.programmerCarl2._10_dynamicProgramming;
import java.util.Arrays;
public class _0062_不同路径 {
}
class Solution0062 {//超时
public int res = 0;
public int uniquePaths(int m, int n) {
dfs(m, n, 1, 1);
return res;
}
public void dfs(int m, int n, int i, int j) {
if (i > m || j > n) {
return;
}
if (i == m && j == n) {
res++;
}
dfs(m, n, i + 1, j);
dfs(m, n, i, j + 1);
}
}
class Solution0062_2 {//超时
public int uniquePaths(int m, int n) {
return dfs(1, 1, m, n);
}
int dfs(int i, int j, int m, int n) {
if (i > m || j > n) //越界了
return 0;
if (i == m && j == n) //找到一种方法,相当于找到了叶子节点
return 1;
return dfs(i + 1, j, m, n) + dfs(i, j + 1, m, n);
}
}
class Solution0062_3 {
/**
* 1. 确定dp数组下标含义 dp[i][j] 到每一个坐标可能的路径种类
* 2. 递推公式 dp[i][j] = dp[i-1][j] dp[i][j-1]
* 3. 初始化 dp[i][0]=1、dp[0][i]=1,初始化横竖就可
* 4. 遍历顺序,一行一行遍历
* 5. 推导结果 。。。。。。。。
*
* @param m
* @param n
* @return
*/
public static int uniquePaths(int m, int n) {
int[][] dp = new int[m][n];
//初始化
for (int i = 0; i < m; i++) {
dp[i][0] = 1;
}
for (int i = 0; i < n; i++) {
dp[0][i] = 1;
}
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}
return dp[m - 1][n - 1];
}
public int uniquePaths2(int m, int n) {//状态压缩
//在二维dp数组中,当前值的计算只依赖正上方和正左方,因此可以压缩成一维数组。
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] = dp[j] (正上方)+ dp[j - 1] (正左方)
}
}
return dp[n - 1];
}
public long uniquePaths3(int m, int n) {//数论
long numerator = 1; // 分子
int denominator = m - 1; // 分母
int count = m - 1;
int t = m + n - 2;
while (count > 0) {
numerator *= (t--);
while (denominator != 0 && numerator % denominator == 0) {
numerator /= denominator;
denominator--;
}
count--;
}
return numerator;
}
}
0063_不同路径II
package com.question.solve.leetcode.programmerCarl2._10_dynamicProgramming;
public class _0063_不同路径II {
}
class Solution0063 {//超时
public int res = 0;
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
dfs(obstacleGrid, 0, 0);
return res;
}
private void dfs(int[][] obstacleGrid, int i, int j) {
if (i >= obstacleGrid.length || j >= obstacleGrid[0].length || obstacleGrid[i][j] == 1) {
return;
}
if (i == obstacleGrid.length - 1 && j == obstacleGrid[0].length - 1) {
res++;
}
dfs(obstacleGrid, i + 1, j);
dfs(obstacleGrid, i, j + 1);
}
}
class Solution0063_2 {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int m = obstacleGrid.length;
int n = obstacleGrid[0].length;
int[][] dp = new int[m][n];
//如果在起点或终点出现了障碍,直接返回0
if (obstacleGrid[m - 1][n - 1] == 1 || obstacleGrid[0][0] == 1) {
return 0;
}
for (int i = 0; i < m && obstacleGrid[i][0] == 0; i++) {
dp[i][0] = 1;
}
for (int j = 0; j < n && obstacleGrid[0][j] == 0; j++) {
dp[0][j] = 1;
}
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
dp[i][j] = (obstacleGrid[i][j] == 0) ? dp[i - 1][j] + dp[i][j - 1] : 0;
}
}
return dp[m - 1][n - 1];
}
//空间优化版本
public int uniquePathsWithObstacles2(int[][] obstacleGrid) {
int m = obstacleGrid.length;
int n = obstacleGrid[0].length;
int[] dp = new int[n];
for (int j = 0; j < n && obstacleGrid[0][j] == 0; j++) {
dp[j] = 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 - 1];
}
}
}
return dp[n - 1];
}
}