当前位置:网站首页>1. < tag dynamic programming and path combination problem > lt.62 Different paths + lt.63 Different paths II
1. < tag dynamic programming and path combination problem > lt.62 Different paths + lt.63 Different paths II
2022-06-23 23:52:00 【Caicai's big data development path】
lt.62. Different paths
[ Case needs ]

[ Train of thought analysis 1 , Abstract as a tree DFS]
[ Code implementation ]
[ Train of thought analysis II , Dynamic programming ]
- Robot from (0,0) Set out , arrive (m-1, n-1) Midpoint ;
- According to the five parts of dynamic rules :
- determine dp The meaning of array and its subscript :
dp[i][j]: From (0, 0) set out , To (i,j) Yes dp[i][j] Different paths
- Determine the recurrence formula
Want to ask for dp[i][j], It can only be derived from two directions ( Down and right ), namely dp[i - 1][j] and dp[i][j - 1];
Take a look back. dp[i - 1][j] What does it mean , It's from (0, 0) It's in (i - 1, j) There are several paths ,dp[i][j - 1] Empathy .
- dp Initialization of an array
How to initialize , First dp[i][0] Some are 1, Because from (0, 0) It's in (i, 0) There's only one way to go , that dp[0][j] Also in the same way . Notice the path , Not the number of steps
So the initialization code is :
for (int i = 0; i < m; i++) dp[i][0] = 1;
for (int j = 0; j < n; j++) dp[0][j] = 1;
- Determine the traversal order
Here's a look at the recursive formula dp[i][j] = dp[i - 1][j] + dp[i][j - 1],dp[i][j] It's all derived from above and to the left , Then it's OK to traverse one layer from left to right .
So you can guarantee that the derivation dp[i][j] When ,dp[i - 1][j] and dp[i][j - 1] There must be a number .
- Give an example to deduce dp Array
[ Code implementation ]
class Solution {
public int uniquePaths(int m, int n) {
//1. dp Array : Go to the (i,j) How many steps are needed
int[][] dp = new int[m][n];
//2. The recursive formula
//dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
//3. dp How to initialize an array
for(int i = 0; i < m; i++){
dp[i][0] = 1;}
for(int j = 0; j < n; j++){
dp[0][j] = 1;}
//4. Determine the traversal order
// From left to right, one floor at a time ( From the top down ) Traverse
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. Give an example to deduce dp Array
}
}
- Complexity of time and space

lt.63. Different paths II
[ Case needs ]

[ Thought analysis ]

[ Code implementation ]
class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
//1. determine dp Array and initialize
//dp[i][j] From [0][0] To [i][j] How many paths are there
int m = obstacleGrid.length;
int n = obstacleGrid[0].length;
// An extra pruning operation
if(obstacleGrid[m - 1][n - 1] == 1 || obstacleGrid[0][0] == 1)return 0;
int[][] dp = new int[m][n];
//2. Deterministic recursion
// dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
//3. The initial value of the array . dp[0][j] = 1; dp[i][0] = 1;
// namely [0][0] Go to his row or column , The paths are all for 1
// This question sets up obstacles , So add judgment ( Where there are no obstacles, there will be initial values )
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. evaluation
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];
}
}
边栏推荐
- 工作中一些常用的工具函数
- Visual explanation of clockwise inner curve in Green's formula hole digging method
- This high imitation millet mall project is amazing
- One person even broke up a Netease cloud music Cloud Village
- Different objects use the same material and have different performances
- Smart doc + Torna compatible version
- How to take the PMP Exam agile on June 25? Share your troubles
- PMP考试相关计算公式汇总!考前必看
- 依赖倒置原则
- 图论(最近公共祖先LCA)
猜你喜欢

How to achieve energy-saving and reasonable lighting control in order to achieve the "double carbon" goal

数字物业管理成趋势,传统物业公司如何通过转型实现数字化蝶变?

电子元器件行业B2B交易管理系统:提升数据化驱动能力,促进企业销售业绩增长

【HackTheBox】Fawn

Postman返回值中文乱码????

Digital property management has become a trend. How can traditional property companies achieve digital butterfly change through transformation?

Task queue of laravel

2.摄像机标定

High imitation Betta app

Smart doc + Torna compatible version
随机推荐
NLP-D58-nlp比赛D27&刷题D14&读论文&mathtype
What kind of automated test is used for H5 mobile terminal
泰勒公式及常用展开
Big guy on security privacy computing escort data security needs to pay attention to science and technology ethics at the same time
Inftnews | where should the future of the creator economy go in the Web3 world?
matlab实现对图像批量重命名
2022山东健博会,济南国际大健康产业博览会,中国营养健康展
牛客网:接雨水的双指针问题
复原IP地址[标准回溯+标准剪枝]
高仿斗鱼 APP
【Try to Hack】masscan
Stm32-------adc (voltage detection)
2022 information security engineer examination knowledge point: access control
Recommend 4 flutter heavy open source projects
Docker Deployment redis
Task queue of laravel
[things about gbase] gbase 8s high availability technology and case analysis (issue 02)
冶金行业数字化供应链管理系统:平台精益化企业管理,助力产业高质量发展
High imitation Betta app
格林公式挖洞法中内曲线顺时针的直观解释
