当前位置:网站首页>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];
}
}
边栏推荐
- MySQL导致索引失效的情况详解
- Loop caused by add of sublist in list
- 如何保证高速公路供电可靠
- 电子元器件行业B2B交易管理系统:提升数据化驱动能力,促进企业销售业绩增长
- List<? extends T>和List<?super T>区别
- 2022山东健博会,济南国际大健康产业博览会,中国营养健康展
- [proteus simulation] example of T6963C driving pg12864 (with Chinese and English display)
- Big guy on security privacy computing escort data security needs to pay attention to science and technology ethics at the same time
- 格林公式挖洞法中内曲线顺时针的直观解释
- Under the background of aging, the comprehensive energy efficiency management platform escorts hospitals
猜你喜欢

Cvpr2019/ image translation: transgaga: geometry aware unsupervised image to image translation

The lower left corner of vs QT VTK displays the synchronized minor coordinate axis

高仿书旗小说 Flutter 版,学起来

How to achieve the turning effect of wechat video recording?

2022 Shandong Health Expo, Jinan International Health Industry Expo, China Nutrition and Health Exhibition

AUTOCAD——总结CAD画圆角的三种方式

Why do MySQL indexes use b+ trees at the bottom? After reading this article, you can easily handle the interview.

Classical Chinese can be programmed???

Detailed explanation of index invalidation caused by MySQL

Taylor formula and common expansion
随机推荐
Flux in three dimensional vector field
Total number of combinations ii[each element can only be solved by + once]
Digital property management has become a trend. How can traditional property companies achieve digital butterfly change through transformation?
Inftnews | where should the future of the creator economy go in the Web3 world?
组合总数II[每个元素只能用一次 + 去重复解集]
2018/GAN:Self-Attention Generative Adversarial Networks自我注意生成对抗网络
生成所有可能的二叉搜索树
docker 部署redis
Solve the problem that slf4j logs are not printed
Generative countermeasure networks (Gans) and variants
Why do MySQL indexes use b+ trees at the bottom? After reading this article, you can easily handle the interview.
Idea automatically generates unit tests, doubling efficiency!
Big guy on security privacy computing escort data security needs to pay attention to science and technology ethics at the same time
Docker redis cluster configuration
Golang 类型断言
Golang type assertion
What kind of automated test is used for H5 mobile terminal
Recommend 4 flutter heavy open source projects
图论(最近公共祖先LCA)
Stm32-------adc (voltage detection)
