当前位置:网站首页>LeetCode_ 63_ Different paths II
LeetCode_ 63_ Different paths II
2022-07-28 18:45:00 【Fitz1318】
Topic link
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 .
Example 1:

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 :
1. towards the right -> towards the right -> Down -> Down
2. Down -> Down -> towards the right -> towards the right
Example 2:

Input :obstacleGrid = [[0,1],[0,0]]
Output :1
Tips :
m == obstacleGrid.lengthn == obstacleGrid[i].length1 <= m, n <= 100obstacleGrid[i][j] by 0 or 1
Their thinking
Dynamic programming
- determine dp The meaning of arrays and subscripts
dp[i][j]: From (0,0) set out , To (i,j) Yesdp[i][j]There are different paths
- Determine the recurrence formula
- Because only from left to right and from top to bottom , therefore
dp[i][j] = dp[i-1][j] + dp[i][j-1] - There are obstacles here
- Because only from left to right and from top to bottom , therefore
- dp Initialization of an array
- The first row and the first column have only one way to walk without encountering obstacles , But after encountering obstacles 0 A way to go
- Determine the traversal order
- Two dimensional array , From left to right , Traverse layer by layer from top to bottom
AC Code
class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int m = obstacleGrid.length;
int n = obstacleGrid[0].length;
int[][] dp = new int[m][n];
for (int i = 0; i < m; i++) {
if (obstacleGrid[i][0] == 1) {
break;
}
dp[i][0] = 1;
}
for (int i = 0; i < n; i++) {
if (obstacleGrid[0][i] == 1) {
break;
}
dp[0][i] = 1;
}
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
if (obstacleGrid[i][j] == 0) {
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
} else {
dp[i][j] = 0;
}
}
}
return dp[m - 1][n - 1];
}
}
边栏推荐
猜你喜欢

Docker builds MySQL master-slave replication

Noise of creative coding

NDK series (5): from introduction to practice, JNI explodes the liver and explains everything in detail!

深圳线下报名|StarRocks on AWS:如何对实时数仓进行极速统一分析

Detailed explanation of oscilloscope parameters

GIS数据漫谈(六)— 投影坐标系统

ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)解决办法

redis优势以及数据结构相关知识

Shenzhen offline registration starrocks on AWS: how to conduct rapid unified analysis of real-time data warehouses

Detailed explanation of network RJ45 interface
随机推荐
Ue5 gas learning notes 1.3 attribute
What is the future of software testing?
Introduction and advanced level of MySQL (6)
leetcode 二叉树类
MYSQL入门与进阶(十)
UE5 GAS 学习笔记 1.9 技能系统全局类(AbilitySystemGlobals)
MongoDB初始化
How does Xiaobai learn software testing with zero foundation?
Look at Devops construction from SRE
Wired: who owns the art of the future? Openai allows dall-e users to commercialize their works. At present
Brief introduction to the principle of spectrometer II
MongoDB数据库复制表
Golang并发模型之
欧美六国最快5日达 菜鸟推出快线产品 优化“端到端”履约服务
NDK series (5): from introduction to practice, JNI explodes the liver and explains everything in detail!
不理解模块化、组件化、插件化的区别怎么行?
What is the employment prospect of software testing?
Pyqt5 rapid development and practice 5.3 multithreading
UE5 GAS 学习笔记 1.7 任务Ability Tasks
LVS manual