当前位置:网站首页>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
边栏推荐
- Jmeter自带函数不够用?不如自己动手开发一个
- Software testing knowledge reserve: how much do you know about the basic knowledge of "login security"?
- rt-thread 中对 hardfault 的处理
- Bypass open_ basedir
- Common problems of caching in high concurrency scenarios
- [SOC FPGA] custom IP PWM breathing lamp
- 博士申请 | 上海交通大学自然科学研究院洪亮教授招收深度学习方向博士生
- 那些自损八百的甲方要求
- vim映射大K
- On the discrimination of "fake death" state of STC single chip microcomputer
猜你喜欢
tkinter窗口选择pcd文件并显示点云(open3d)
JVM monitoring and diagnostic tools - command line
Sequential storage of stacks
[FPGA tutorial case 13] design and implementation of CIC filter based on vivado core
dolphinscheduler3.x本地启动
[FPGA] EEPROM based on I2C
C note 13
JVM监控及诊断工具-命令行篇
Navicat导入15G数据报错 【2013 - Lost connection to MySQL server during query】 【1153:Got a packet bigger】
Dc-7 target
随机推荐
Navicat导入15G数据报错 【2013 - Lost connection to MySQL server during query】 【1153:Got a packet bigger】
「解析」FocalLoss 解决数据不平衡问题
Oracle迁移中关于大容量表使用数据泵(expdp、impdp)导出导入容易出现的问题和注意事项
Apple CMS V10 template /mxone Pro adaptive film and television website template
Rk3399 platform development series explanation (WiFi) 5.53, hostapd (WiFi AP mode) configuration file description
进程间通信之共享内存
Cloud acceleration helps you effectively solve attack problems!
直击2022ECDC萤石云开发者大会:携手千百行业加速智能升级
ICML 2022 | explore the best architecture and training method of language model
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
Chain storage of stack
Laravel uses Tencent cloud cos5 full tutorial
laravel 使用腾讯云 COS5全教程
骑士战胜魔王(背包&dp)
为不同类型设备构建应用的三大更新 | 2022 I/O 重点回顾
微信小程序隐藏video标签的进度条组件
面试中有哪些经典的数据库问题?
[shell] summary of common shell commands and test judgment statements
JMeter function assistant - random value, random string, fixed value random extraction
基本Dos命令