当前位置:网站首页>不同路径II[针对DFS的动态规划改进]
不同路径II[针对DFS的动态规划改进]
2022-06-25 06:39:00 【REN_林森】
前言
动态规划,常见的空间换时间,尤其是针对DFS而言,减少很多不必要的重复计算。
一、不同路径II
二、DP + 原地空间利用
package everyday.medium;
// 不同路径II
public class UniquePathsWithObstacles {
/* target:从矩阵左上角走到右下角,可能的走法,有障碍物的地方走不了。 第一想法是DFS+越过障碍物,但是DFS有很多重复的子路径,所以可以空间换时间,用动态规划。 每个格子都是从左或上走过来的,所以可以一步步计算走到右下角有多少种走法。 // 注:小技巧,原地修改矩阵,利用起来,减少空间复杂度。 */
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
// 由于1表示障碍,0表示无障碍,所以原地修改矩阵时,用负数表示路径可能数。
int m = obstacleGrid.length, n = obstacleGrid[0].length;
// 特殊情况,如果起点和终点都是障碍物,就不用走了。
if (obstacleGrid[0][0] == 1 || 1 == obstacleGrid[m - 1][n - 1]) return 0;
obstacleGrid[0][0] = -1;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
// 如果是障碍物,则不必计算。
if (obstacleGrid[i][j] == 1) continue;
int up = i == 0 || obstacleGrid[i - 1][j] == 1 ? 0 : obstacleGrid[i - 1][j];
int left = j == 0 || obstacleGrid[i][j - 1] == 1 ? 0 : obstacleGrid[i][j - 1];
// 当前格子为终点,可能的路径数为 左边走过来的 + 右边走过来的。
// bug1:当i == j == 0时,初始化的-1也被覆盖了,全部为0
// obstacleGrid[i][j] = left + up;
obstacleGrid[i][j] += left + up;
}
}
// 返回以右下角为终点,可能的路径数。
return -obstacleGrid[m - 1][n - 1];
}
}
总结
1)动态规划。
参考文献
[1] LeetCode 不同路径II
边栏推荐
- Icon already includes gloss effects
- Too beautiful promise because too young
- 威迈斯新能源冲刺科创板:年营收17亿 应收账款账面价值近4亿
- One year's time and University experience sharing with CSDN
- Alphassl wildcard certificate for one month
- Cocos learning diary 3 - API acquisition nodes and components
- Harmony food menu interface
- Orcad Schematic常用功能
- Keepalived monitors the process and automatically restarts the service process
- [batch dos-cmd command - summary and summary] - add comment command (REM or::)
猜你喜欢
Sichuan earth microelectronics ca-is1200 isolated operational amplifier for current detection
Loopholes in the missed scanning system of Lvmeng and its repair scheme
Zhugeliang vs pangtong, taking distributed Paxos
Hanxin's trick: consistent hashing
From perceptron to transformer, a brief history of deep learning
【批處理DOS-CMD命令-匯總和小結】-外部命令-cmd下載命令、抓包命令(wget)
14 bs对象.节点名称.name attrs string 获取节点名称 属性 内容
Weimeisi new energy rushes to the scientific innovation board: the annual revenue is 1.7 billion, and the book value of accounts receivable is nearly 400million
【批处理DOS-CMD命令-汇总和小结】-CMD窗口的设置与操作命令(cd、title、mode、color、pause、chcp、exit)
Orcad Schematic常用功能
随机推荐
How do I create a guid in excel- How to create a GUID in Excel?
Domestic MCU perfectly replaces STM chip model of Italy France
【UVM入門 ===> Episode_9 】~ 寄存器模型、寄存器模型的集成、寄存器模型的常規方法、寄存器模型的應用場景
My debut is finished!
Harmony food menu interface
313. Binary sum
[batch dos-cmd command - summary and summary] - CMD extended command and function (CMD /e:on, CMD /e:off)
Google extender address
Using awk to process input from multiple files
【批处理DOS-CMD命令-汇总和小结】-添加注释命令(rem或::)
Flexbox on ie11: stretching images for no reason- Flexbox on IE11: image stretched for no reason?
The perfect presentation of Dao in the metauniverse, and platofarm creates a farm themed metauniverse
【LeetCode】two num·两数之和
【批处理DOS-CMD命令-汇总和小结】-cmd扩展命令、扩展功能(cmd /e:on、cmd /e:off)
Finally, when you open source the applet ~
SQL query, if value is null then return 1 - SQL query, if value is null then return 1
Conditional grouping with $exists inside $cond
Chang Wei (variables and constants) is easy to understand
Authentique Photoshop 2022 expérience d'achat partage
How to get the difference between two dates rounded to hours