当前位置:网站首页>力扣62 不同路径(从矩阵左上到右下的所有路径数量) (动态规划)
力扣62 不同路径(从矩阵左上到右下的所有路径数量) (动态规划)
2022-07-07 01:41:00 【裴南苇_】
今天遇到一道个人认为很有价值的动态规划问题。从这道题里,个人感觉对动态规划有了更深刻的了解,大家可以看看对自己又没有帮助。
先看题目:
一个机器人位于一个 m x n 网格的左上角,机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角问总共有多少条不同的路径?
示例
输入:m = 3, n = 2
输出:3
解释:
从左上角开始,总共有 3 条路径可以到达右下角。
1. 向右 -> 向下 -> 向下
2. 向下 -> 向下 -> 向右
3. 向下 -> 向右 -> 向下
解答:
由题目得知,每一次只能向下或者向右移动
由此我们推知 ===》 到达一个格子的路径数量,由它左边格子和上边格子所决定
由此我们推知 ===》 这里可以使用动态规划的思想来解决
由此动态方程 ===》 dp[i][j] = dp[i-1][j] + dp[i][j-1]
至此,我们已经解决一半的问题了,然后我们再思考第一列和第一行,由于每一次只能向下或者向右移动,所以第一行的每个格子都只能由它左边的格子向右移动而来, 所以第一行dp[0][j] 都应该为1,第一列格子同理。
至此分析结束,列出代码
public int uniquePaths(int m, int n) {
int[][] dp = new int [m][n];
for(int i=0;i<n;i++){
dp[0][i] = 1;
}
for(int i=0;i<m;i++){
dp[i][0] = 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];
}

好了,这次的文章就到这里,喜欢的同学可以点赞收藏,遇到问题,可以评论,或者留言,我一定会第一时间给到回馈,感谢观看!!
注:本文为本人学习时心得分享,有讲错或者需要改正的地方,请指正,我会虚心接受
边栏推荐
- 基于FPGA的VGA协议实现
- [SOC FPGA] custom IP PWM breathing lamp
- K8s running Oracle
- 绕过open_basedir
- Personal imitation SSM framework
- You don't know the complete collection of recruitment slang of Internet companies
- 【GNN】图解GNN: A gentle introduction(含视频)
- PowerPivot - DAX (function)
- JMeter function assistant - random value, random string, fixed value random extraction
- The solution of a simple algebraic problem
猜你喜欢

为不同类型设备构建应用的三大更新 | 2022 I/O 重点回顾

Jstat of JVM command: View JVM statistics

JVM命令之 jstat:查看JVM统计信息

【FPGA教程案例14】基于vivado核的FIR滤波器设计与实现

The boss always asks me about my progress. Don't you trust me? (what do you think)

进程间通信之共享内存

Check point: the core element for enterprises to deploy zero trust network (ztna)

Chain storage of stack

Markdown 并排显示图片

JVM命令之- jmap:导出内存映像文件&内存使用情况
随机推荐
高并发大流量秒杀方案思路
DC-7靶机
ML's shap: Based on the adult census income binary prediction data set (whether the predicted annual income exceeds 50K), use the shap decision diagram combined with the lightgbm model to realize the
If you don't know these four caching modes, dare you say you understand caching?
Bypass open_ basedir
Check point: the core element for enterprises to deploy zero trust network (ztna)
10W word segmentation searches per second, the product manager raised another demand!!! (Collection)
对称的二叉树【树的遍历】
ST表预处理时的数组证明
Financial risk control practice - decision tree rule mining template
360 Zhiyu released 7.0 new products to create an exclusive "unified digital workspace" for the party, government and army, and central and state-owned enterprises
window下面如何安装swoole
Find duplicate email addresses
When we talk about immutable infrastructure, what are we talking about
JVM command - jmap: export memory image file & memory usage
Oracle迁移中关于大容量表使用数据泵(expdp、impdp)导出导入容易出现的问题和注意事项
The boss always asks me about my progress. Don't you trust me? (what do you think)
Value range of various datetimes in SQL Server 2008
Experience sharing of contribution of "management world"
Cloud acceleration helps you effectively solve attack problems!