当前位置:网站首页>Different paths ii[dynamic planning improvement for DFS]
Different paths ii[dynamic planning improvement for DFS]
2022-06-25 07:40:00 【REN_ Linsen】
Preface
Dynamic programming , Common space for time , Especially for DFS for , Reduce a lot of unnecessary double counting .
One 、 Different paths II

Two 、DP + In situ space utilization
package everyday.medium;
// Different paths II
public class UniquePathsWithObstacles {
/* target: Go from the upper left corner of the matrix to the lower right corner , Possible ways , You can't walk where there are obstacles . The first idea is DFS+ Over obstacles , however DFS There are many repeated sub paths , So you can exchange space for time , With dynamic programming . Each grid comes from the left or the top , So you can calculate how many ways to walk to the lower right corner step by step . // notes : Tips , In situ modification matrix , Use it , Reduce space complexity . */
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
// because 1 Indicates an obstacle ,0 Indicates accessibility , So when modifying the matrix in place , Use a negative number to indicate the possible number of paths .
int m = obstacleGrid.length, n = obstacleGrid[0].length;
// A special case , If both the starting point and the ending point are obstacles , You don't have to go .
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 it's an obstacle , Then it is not necessary to calculate .
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];
// The current grid is the end point , The number of possible paths is Coming from the left + On the right .
// bug1: When i == j == 0 when , The initialization of the -1 Also covered , All for 0
// obstacleGrid[i][j] = left + up;
obstacleGrid[i][j] += left + up;
}
}
// Return to the bottom right corner , Number of possible paths .
return -obstacleGrid[m - 1][n - 1];
}
}
summary
1) Dynamic programming .
reference
边栏推荐
- What if there is no point in data visualization?
- Chuantuwei ca-is3720lw alternative material No. iso7820fdw
- 一“石”二“鸟”,PCA有效改善机载LiDAR林下地面点部分缺失的困局
- Tempest HDMI leak receive 1
- npm install 报错 : gyp ERR! configure error
- Harmony food menu interface
- Debian introduction
- C#入门教程
- 对链表进行插入排序[dummy统一操作+断链核心--被动节点]
- This year, I graduated
猜你喜欢

几款不错的天气插件

正版photoshop2022购买体验经历分享

How comfortable it is to use Taijiquan to talk about distributed theory!

【UVM入门 ===> Episode_9 】~ 寄存器模型、寄存器模型的集成、寄存器模型的常规方法、寄存器模型的应用场景

LeetCode 每日一题——515. 在每个树行中找最大值

OAuth 2.0一键登录那些事

FairMOT yolov5s转onnx

Genuine photoshop2022 purchase experience sharing

【LeetCode】two num·两数之和

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
随机推荐
Intel announced five new technological developments, including quantum computing, neural pseudo computing, machine programming, integrated optoelectronics, and secure computing
OpenMP入门
Construction of occupancy grid map
用太极拳讲分布式理论,真舒服!
[introduction to UVM== > episode_9] ~ register model, integration of register model, general methods of register model, application scenarios of register model
Sichuan Tuwei ca-if1051 can transceiver has passed aec-q100 grade 1 certification
smartBugs安装小问题总结
13 `bs_duixiang.tag标签`得到一个tag对象
Tuwei Digital Isolator and interface chip can perfectly replace imported brands Ti and ADI
Home environment monitoring system design (PC version) (mobile app version to be determined)
[batch dos-cmd command - summary and summary] - file and directory operation commands (MD, RD, xcopy, dir, CD, set, move, copy, del, type, sort)
Three years of continuous decline in revenue, Tiandi No. 1 is trapped in vinegar drinks
Cocos学习日记3——api获取节点、组件
Distributed quorum NWR of the alchemy furnace of the Supreme Master
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命令-汇总和小结】-添加注释命令(rem或::)
Unity3D邪门实现之GUI下拉菜单Dropdown设计无重复项
无“米”,也能煮“饭”利用“点云智绘”反演机载LiDAR林下缺失地面点攻略
Debian introduction
Runtime——methods成员变量,cache成员变量