当前位置:网站首页>Leetcode shahutong series -- 63. Different paths II
Leetcode shahutong series -- 63. Different paths II
2022-07-26 00:02:00 【On the River continent, Mu Shui】
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.length
n == obstacleGrid[i].length
1 <= m, n <= 100
obstacleGrid[i][j] by 0 or 1
source : Power button (LeetCode)
link :https://leetcode.cn/problems/unique-paths-ii
Ideas :
Define a two-dimensional array dp[][] ,dp[i][j] From top left to top left grid (i,j) How many paths are there in the location
1. initialization
1.1) dp[0][0] = 1
1.2) dp[0][j] j > 0 && j < n,
if grid[0][j] = 0 , be dp[0][j] = 1,
if grid[0][j] = 1,dp[0][j] = 0, ..., dp[0][n-1] = 0
1.3) dp[i][0], i > 0 && i < m
if grid[i][0] = 0 , be dp[i][0] = 1,
if grid[i][0] = 1,dp[i][0] = 0, ..., dp[m-1][n0] = 0
2. State shift
if grid[i][j] == 1, be dp[i][j] = 0; That the road is blocked
if grid[i][j] == 0, dp[i][j] = dp[i-1][j] + dp[i-1][j]
3. Return results dp[m-1][n-1]java Code :
class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int m = obstacleGrid.length;
int n = obstacleGrid[0].length;
int[][] dp = new int[m][n];
// 1. initialization
boolean flag1 = true;
for (int i = 0; i < m; i++) {
if (flag1 && obstacleGrid[i][0] == 0) {
dp[i][0] = 1;
} else {
flag1 = false;
}
}
boolean flag2 = true;
for (int j = 0; j < n; j++) {
if (flag2 && obstacleGrid[0][j] == 0) {
dp[0][j] = 1;
} else {
flag2 = false;
}
}
// 2. State shift
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
if(obstacleGrid[i][j] == 1) {
dp[i][j] = 0; // That the road is blocked
} else {
dp[i][j] = dp[i-1][j] + dp[i][j-1];
}
}
}
// 3. Return results
return dp[m-1][n-1];
}
}边栏推荐
- 【一库】mapbox-gl!一款开箱即用的地图引擎
- 调用钉钉api报错:机器人发送签名过期;solution:签名生成时间和发送时间请保持在 timestampms 以内
- GUI interface of yolov3 (2) -- beautify the page + output the name and quantity of the identified object
- 模式之固定与交替顺序执行
- Interview focus - TCP protocol of transport layer
- 程序环境和预处理
- Article 75: writing skills of academic papers
- 栈与队列——239. 滑动窗口最大值
- 二叉树——700.二叉搜索树中的搜索
- The expression of flag=false if (flag) {return} timer=null if (timer) {return} in the throttle valve has been unclear
猜你喜欢
随机推荐
Sort fake contacts
二叉树——617. 合并二叉树
The GUI interface of yolov3 (3) -- solve the out of memory problem and add camera detection function
Native JS perfectly realizes deep copy
【ManageEngine】ServiceDesk Plus荣获2022安全样板工程数据安全奖
什么叫做 inode ?带你理解 inode 和对于创建文件和删除文件时 inode 都提供了哪些帮助。
Quick sorting of top ten sorting
Macro task, micro task and event cycle mechanism
Vscode format JSON file
Call nailing API and report an error: the signature sent by the robot is expired; Solution: please keep the signature generation time and sending time within timestampms
Kubernetes网络插件详解 - Calico篇 - 概述
Prometheus 运维工具 Promtool (二) Query 功能
牛客/洛谷——[NOIP2003 普及组]栈
复盘:推荐系统—— 负采样策略
NVIDIA可编程推理加速器TensorRT学习笔记(三)——加速推理
如何用yolov5 做个闯红灯监控的智能交通系统(1)
Taobao flexible.js file realizes flexible layout
你还在用浏览器自带书签?这款书签插件超赞
Chapter 64: error lnk2019: unresolved external symbol cvround
MySQL的DDL、DML和DQL的基本语法







![[learning notes] unreal 4 engine introduction (IV)](/img/30/4defa3cbd785d43adb405c71d16406.png)
![[learning notes] solid works operation record](/img/f7/0535b473755643ce32996f00fa5554.png)
