当前位置:网站首页>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
边栏推荐
- Ideas of high concurrency and high traffic seckill scheme
- [FPGA tutorial case 14] design and implementation of FIR filter based on vivado core
- 软件测试的几个关键步骤,你需要知道
- CloudCompare-点对选取
- ceres-solver和g2o性能比较
- 缓存在高并发场景下的常见问题
- Find duplicate email addresses
- DC-7靶机
- matlab / ENVI 主成分分析实现及结果分析
- Array proof during st table preprocessing
猜你喜欢
Doctoral application | Professor Hong Liang, Academy of natural sciences, Shanghai Jiaotong University, enrolls doctoral students in deep learning
Several key steps of software testing, you need to know
What are the classic database questions in the interview?
string(讲解)
「解析」FocalLoss 解决数据不平衡问题
Go language learning notes - Gorm use - native SQL, named parameters, rows, tosql | web framework gin (IX)
Experience sharing of contribution of "management world"
哈趣投影黑馬之姿,僅用半年强勢突圍千元投影儀市場!
3428. Put apples
Handling hardfault in RT thread
随机推荐
3531. 哈夫曼树
牛客小白月赛52 E.分组求对数和(二分&容斥)
Rk3399 platform development series explanation (WiFi) 5.52. Introduction to WiFi framework composition
Ctfshow-- common posture
一段程序让你明白什么静态内部类,局部内部类,匿名内部类
HKUST & MsrA new research: on image to image conversion, fine tuning is all you need
ETCD数据库源码分析——从raftNode的start函数说起
[shell] summary of common shell commands and test judgment statements
为不同类型设备构建应用的三大更新 | 2022 I/O 重点回顾
Crudini 配置文件编辑工具
rt-thread 中对 hardfault 的处理
「解析」FocalLoss 解决数据不平衡问题
2022Android面试必备知识点,一文全面总结
对称的二叉树【树的遍历】
Ant manor safety helmet 7.8 ant manor answer
JVM监控及诊断工具-命令行篇
Crudini profile editing tool
jmeter 函数助手 — — 随机值、随机字符串、 固定值随机提取
You don't know the complete collection of recruitment slang of Internet companies
Jstat of JVM command: View JVM statistics