当前位置:网站首页>Force deduction 62 different paths (the number of all paths from the upper left to the lower right of the matrix) (dynamic planning)
Force deduction 62 different paths (the number of all paths from the upper left to the lower right of the matrix) (dynamic planning)
2022-07-07 06:24:00 【Pei Nanwei_】
Today I come across a dynamic programming problem that I think is very valuable . From this question , I feel I have a deeper understanding of Dynamic Planning , You can see that it doesn't help you .
First look at the topic :
A robot is in a m x n The top left corner of the grid , The robot can only move down or right one step at a time . The robot tries to reach the lower right corner of the grid and asks how many different paths there are ?
Example
Input :m = 3, n = 2
Output :3
explain :
From the top left corner , All in all 3 Path to the bottom right .
1. towards the right -> Down -> Down
2. Down -> Down -> towards the right
3. Down -> towards the right -> Down
answer :
From the title , You can only move down or right at a time
From this we infer ===》 The number of paths to a grid , Determined by its left grid and upper grid
From this we infer ===》 Here we can use the idea of dynamic programming to solve
From this dynamic equation ===》 dp[i][j] = dp[i-1][j] + dp[i][j-1]
thus , We have solved half the problem , Then we think about the first column and the first row , Because you can only move down or right at a time , therefore Each grid in the first row can only be moved to the right from the grid on its left , So the first line dp[0][j] All for 1, The first column of lattice is the same .
This concludes the analysis , List codes
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];
}

Okay , That's all for this article , Favorite students can like the collection , Have a problem , Can comment , Or leave a message , I will give you feedback at the first time , Thank you for watching. !!
notes : This article is for me to share my learning experience , There are mistakes or areas that need to be corrected , Please correct me. , I will accept with an open mind
边栏推荐
- 那些自损八百的甲方要求
- Markdown displays pictures side by side
- 当我们谈论不可变基础设施时,我们在谈论什么
- Symmetric binary tree [tree traversal]
- MySQL(十)
- matlab / ENVI 主成分分析实现及结果分析
- Ctfshow-- common posture
- Ha Qu projection dark horse posture, only half a year to break through the 1000 yuan projector market!
- 360织语发布7.0新品 为党政军、央国企打造专属“统一数字工作空间”
- c面试 加密程序:由键盘输入明文,通过加密程序转换成密文并输出到屏幕上。
猜你喜欢

Handling hardfault in RT thread

Laravel uses Tencent cloud cos5 full tutorial

On the discrimination of "fake death" state of STC single chip microcomputer

Jmeter自带函数不够用?不如自己动手开发一个

@Detailed differences between pathvariable and @requestparam

【OpenCV】形态学滤波(2):开运算、形态学梯度、顶帽、黑帽

Common problems of caching in high concurrency scenarios

JMeter's own functions are not enough? Why don't you develop one yourself

You don't know the complete collection of recruitment slang of Internet companies

ETCD数据库源码分析——从raftNode的start函数说起
随机推荐
rt-thread 中对 hardfault 的处理
【OpenCV】形态学滤波(2):开运算、形态学梯度、顶帽、黑帽
Qtthread, one of many methods of QT multithreading
Dc-7 target
Redisl garbled code and expiration time configuration
3428. 放苹果
Qt多线程的多种方法之一 QThread
Wechat applet hides the progress bar component of the video tag
哈趣投影黑马之姿,仅用半年强势突围千元投影仪市场!
string(讲解)
Navicat导入15G数据报错 【2013 - Lost connection to MySQL server during query】 【1153:Got a packet bigger】
VMware安装后打开就蓝屏
window下面如何安装swoole
软件测试的几个关键步骤,你需要知道
Test the foundation of development, and teach you to prepare for a fully functional web platform environment
C语言面试 写一个函数查找两个字符串中的第一个公共字符串
蚂蚁庄园安全头盔 7.8蚂蚁庄园答案
Sequential storage of stacks
线性代数(一)
Ideas of high concurrency and high traffic seckill scheme