当前位置:网站首页>1.< tag-动态规划和路径组合问题>lt.62. 不同路径 + lt.63. 不同路径 II
1.< tag-动态规划和路径组合问题>lt.62. 不同路径 + lt.63. 不同路径 II
2022-06-23 22:18:00 【菜菜的大数据开发之路】
lt.62. 不同路径
[案例需求]

[思路分析一, 抽象为树进行DFS]
[代码实现]
[思路分析二, 动态规划]
- 机器人从(0,0)位置出发, 到达(m-1, n-1)中点;
- 按照动规五部曲来看:
- 确定dp数组及其下标的含义:
dp[i][j]: 表示从(0, 0) 出发, 到(i,j)有dp[i][j]条不同的路径
- 确定递推公式
想要求dp[i][j], 只能由两个方向推导出来(即向下和向右), 即dp[i - 1][j] 和 dp[i][j - 1];
回顾一下 dp[i - 1][j] 表示啥,是从(0, 0)的位置到(i - 1, j)有几条路径,dp[i][j - 1]同理。
- dp数组的初始化
如何初始化呢,首先dp[i][0]一定都是1,因为从(0, 0)的位置到(i, 0)的路径只有一条,那么dp[0][j]也同理。 注意是路径奥, 可不是步数
所以初始化代码为:
for (int i = 0; i < m; i++) dp[i][0] = 1;
for (int j = 0; j < n; j++) dp[0][j] = 1;
- 确定遍历顺序
这里要看一下递归公式dp[i][j] = dp[i - 1][j] + dp[i][j - 1],dp[i][j]都是从其上方和左方推导而来,那么从左到右一层一层遍历就可以了。
这样就可以保证推导dp[i][j]的时候,dp[i - 1][j] 和 dp[i][j - 1]一定是有数值的。
- 举例推导dp数组
[代码实现]
class Solution {
public int uniquePaths(int m, int n) {
//1. dp数组: 走到(i,j)处需要多少步
int[][] dp = new int[m][n];
//2. 递推公式
//dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
//3. dp数组如何初始化
for(int i = 0; i < m; i++){
dp[i][0] = 1;}
for(int j = 0; j < n; j++){
dp[0][j] = 1;}
//4. 确定遍历顺序
//从左往右一层一层(从上往下)遍历
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];
//5. 举例推导dp数组
}
}
- 时空复杂度

lt.63. 不同路径 II
[案例需求]

[思路分析]

[代码实现]
class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
//1. 确定dp数组的功能并初始化
//dp[i][j]表示从[0][0] 到[i][j]有多少条路径
int m = obstacleGrid.length;
int n = obstacleGrid[0].length;
//额外的一个剪枝操作
if(obstacleGrid[m - 1][n - 1] == 1 || obstacleGrid[0][0] == 1)return 0;
int[][] dp = new int[m][n];
//2. 确定递推
// dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
//3. 数组的初值. dp[0][j] = 1; dp[i][0] = 1;
//即 [0][0]到他所在的行或者是列上, 路径都是为1
//本题设置了障碍, 所以要加上判断(没有障碍的地方才会有初值)
for(int i = 0; i < m; i++){
if(obstacleGrid[i][0] == 1)break;
dp[i][0] = 1;
}
for(int j = 0; j < n; j++){
if(obstacleGrid[0][j] == 1)break;
dp[0][j] = 1;
}
//4. 求值
for(int i = 1; i < m; i++){
for(int j = 1; j < n; j++){
if(obstacleGrid[i][j] != 1){
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}
}
return dp[m - 1][n - 1];
}
}
边栏推荐
- 微信录制视频转圈效果如何实现?
- 2022 point de connaissance de l'examen des ingénieurs en sécurité de l'information: contrôle d'accès
- Chrome plug-in features and case analysis of actual combat scenarios
- AUTOCAD——总结CAD画圆角的三种方式
- One person even broke up a Netease cloud music Cloud Village
- MySQL索引底层为什么用B+树?看完这篇文章,轻松应对面试。
- 项目中常用到的 19 条 MySQL 优化
- 3D打印和激光切割流程的初步了解
- 2022山东健博会,济南国际大健康产业博览会,中国营养健康展
- Thinking (87): Protocol encryption and compression
猜你喜欢

CVPR2019/图像翻译:TransGaGa: Geometry-Aware Unsupervised Image-to-Image Translation几何感知的无监督图像到图像的翻译

Why do MySQL indexes use b+ trees at the bottom? After reading this article, you can easily handle the interview.

【HackTheBox】Fawn

Kotlin coroutine asynchronous flow

Autofac details

MySQL导致索引失效的情况详解

Autofac详解

老龄化下背景下,综合能效管理平台为医院保驾护航

电子元器件行业B2B交易管理系统:提升数据化驱动能力,促进企业销售业绩增长
![[Xilinx ax7103 microbalze Learning Notes 6] MicroBlaze custom IP core packaging experiment](/img/26/c4e6be2e3b17c7925108e95943c558.png)
[Xilinx ax7103 microbalze Learning Notes 6] MicroBlaze custom IP core packaging experiment
随机推荐
Golang 类型断言
2018/GAN:Self-Attention Generative Adversarial Networks自我注意生成对抗网络
【Try to Hack】masscan
PyQt5_QTableWidget分页单选右键菜单控件
Can the characteristics of different network structures be compared? Ant & meituan & NTU & Ali proposed a cross architecture self supervised video representation learning method CaCl, performance SOTA
产线工控安全有什么好的解决方案
工作中一些常用的工具函數
Install using snap in opencloudos NET 6
微信小程序 图片验证码展示
High imitation Book flag novel flutter edition, learn it
Some common tool functions in work
Kotlin set list, set, map operation summary
ACM. HJ89 24点运算 ●●●
A cartoon reading app highly imitating Tencent comics
2022 information security engineer examination knowledge point: access control
三款很酷很骚气的底部导航
Idea automatically generates unit tests, doubling efficiency!
1004. 最大连续1的个数 III ●●
Niuke.com: the double pointer problem of receiving rainwater
VS QT VTK 左下角显示同步小坐标轴
