当前位置:网站首页>Dynamic planning - 63. Different paths II
Dynamic planning - 63. Different paths II
2022-07-28 03:44:00 【Xiao Zhao, who is working hard for millions of annual salary】
1 Title Description
A robot is in a m x n The top left corner of the grid ( The starting point is marked as “Start” ).
The robot can only move down or right one step at a time . The robot tries to reach the bottom right corner of the grid ( In the figure below, it is marked as “Finish”).
Now consider the obstacles in the grid . So how many different paths will there be from the top left corner to the bottom right corner ?
Obstacles and empty positions in the grid are used respectively 1 and 0 To express .
source : Power button (LeetCode)
link :https://leetcode.cn/problems/unique-paths-ii
2 Title Example

Input :obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]]
Output :2
explain :3x3 There is an obstacle in the middle of the grid .
From the top left corner to the bottom right corner, there are 2 Different paths :
- towards the right -> towards the right -> Down -> Down
- Down -> Down -> towards the right -> towards the right

Input :obstacleGrid = [[0,1],[0,0]]
Output :1
3 Topic tips
m == obstacleGrid.length
n == obstacleGrid[i].length
1 <= m, n <= 100
obstacleGrid[i][j] by 0 or 1
4 Ideas
This question is relative to 62. Different paths (opens new window) There are obstacles .
Students who are exposed to this topic for the first time may be a little confused , There's an obstacle , What should I do ?
62. Different paths (opens new window) In, we have analyzed the situation of no obstacles in detail , If there are obstacles , In fact, it is the corresponding mark dp table(dp Array ) Keep the initial value (0) That's all right. .
Five steps of dynamic planning :
determine dp Array (dp table) And the meaning of subscripts
dp[i][j] : From (0 ,0) set out , To (i, j) Yes dp[i][j] Different paths .
Determine the recurrence formula
Recurrence formula and 62. Different paths are the same ,dp[i][j] = dp[i - 1][j] + dp[i][j - 1].
But here's a point to note , Because there are obstacles ,(i, j) If it's an obstacle, you should keep the initial state ( The initial state is 0).
So the code is :
if (obstacleGrid[i][j] == 0) {
// When (i, j) When there are no obstacles , Re derivation dp[i][j]
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
From recursive formula dp[i][j] = dp[i - 1][j] + dp[i][j - 1] It can be seen that , It must be traversing from left to right , This ensures that the derivation dp[i][j] When ,dp[i - 1][j] and dp[i][j - 1] There must be a value .
The subject is 62. Different paths (opens new window) Obstacle version of , The overall thinking is generally the same .
But even if I did 62. Different paths , When doing this problem, you will also feel that you have no way to start when you encounter obstacles .
In fact, just consider , Encounter obstacles dp[i][j] keep 0 That's all right. .
There are also some small details , for example : Initialization part , It's easy to ignore the obstacles. They should all be 0 The situation of .
The idea of this topic is taken from the code Capriccio
link :https://programmercarl.com/0063.%E4%B8%8D%E5%90%8C%E8%B7%AF%E5%BE%84II.html#%E6%80%9D%E8%B7%AF
5 My answer
class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int n = obstacleGrid.length, m = obstacleGrid[0].length;
int[][] dp = new int[n][m];
for (int i = 0; i < m; i++) {
if (obstacleGrid[0][i] == 1) break; // Once you encounter obstacles , The follow-up will not arrive
dp[0][i] = 1;
}
for (int i = 0; i < n; i++) {
if (obstacleGrid[i][0] == 1) break; Once you encounter obstacles , The follow-up will not arrive
dp[i][0] = 1;
}
for (int i = 1; i < n; i++) {
for (int j = 1; j < m; j++) {
if (obstacleGrid[i][j] == 1) continue;
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}
return dp[n - 1][m - 1];
}
}
边栏推荐
- MSGAN用于多种图像合成的模式搜索生成对抗网络---解决模式崩塌问题
- Volvo: what on earth does the deep-rooted "sense of security" rely on?
- Push chart written by helm to harbor warehouse
- The wonderful use of asemi rectifier bridge GBPC3510 in DC brush motor
- LeetCode 0140. 单词拆分 II
- 动态规划——416. 分割等和子集
- LeetCode_ 409_ Longest palindrome string
- 动态规划——509. 斐波那契数
- WordPress简约mkBlog博客主题模板v2.1
- Use of custom annotations
猜你喜欢

一文读懂Plato Farm的ePLATO,以及其高溢价缘由

Super nice PHP program source code of nteam official website

【LeetCode】34、在排序数组中查找元素的第一个和最后一个位置

leetcode刷题:动态规划08(分割等和子集)

The latest version of pagoda installs the zip extension, and PHP -m does not display the processing method

Capacity expansion and reduction of RBD block storage device (VI)

【力扣】1337.矩阵中战斗力最弱的k行

Outlook tutorial, how to use color categories and reminders in outlook?
![[leetcode] 34. Find the first and last positions of elements in the sorted array](/img/f0/3eaa33fa7b13abe5f27b136239507d.png)
[leetcode] 34. Find the first and last positions of elements in the sorted array

单调栈——739. 每日温度
随机推荐
[错题]Concatenation
Leetcode skimming: dynamic programming 08 (segmentation and subsets)
Greedy - 53. Maximum subarray sum
数据丰富的计算:M.2在边缘遇到AI
Qt:qmessagebox message box, custom signal and slot
LeetCode 0140. 单词拆分 II
Qt:QMessageBox消息框、自定义信号和槽
Xctf attack and defense world web master advanced area php2
【OPENVX】对象基本使用之vx_matrix
Data rich Computing: m.2 meets AI at the edge
95后阿里P7晒出工资单:真的是狠狠扎心了...
Digital economy has become the biggest attraction
The latest version of pagoda installs the zip extension, and PHP -m does not display the processing method
高等数学(第七版)同济大学 习题3-4 个人解答(后8题)
[错题]Mocha and Railgun
构建“产业大脑”,以“数字化”提升园区运营管理及服务能力!
【OPENVX】对象基本使用之vx_distribution
LeetCode_409_最长回文串
WordPress简约mkBlog博客主题模板v2.1
WordPress simple mkblog blog theme template v2.1