问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3682 访问。

一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” )。

机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。

现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径?

网格中的障碍物和空位置分别用 1 和 0 来表示。

说明:m 和 n 的值均不超过 100。

输入: [

[0,0,0],

[0,1,0],

[0,0,0]

]

输出: 2

解释: 3x3 网格的正中间有一个障碍物。从左上角到右下角一共有 2 条不同的路径:

  1. 向右 -> 向右 -> 向下 -> 向下
  2. 向下 -> 向下 -> 向右 -> 向右

A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the diagram below).

Now consider if some obstacles are added to the grids. How many unique paths would there be?

An obstacle and empty space is marked as 1 and 0 respectively in the grid.

Note: m and n will be at most 100.

Input: [

[0,0,0],

[0,1,0],

[0,0,0]

]

Output: 2 Explanation:

There is one obstacle in the middle of the 3x3 grid above. There are two ways to reach the bottom-right corner:

  1. Right -> Right -> Down -> Down
  2. Down -> Down -> Right -> Right

示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3682 访问。

public class Program {

    public static void Main(string[] args) {
var obstacleGrid = new int[,] {
{ 0, 0, 0 },
{ 0, 1, 0 },
{ 0, 0, 0 }
}; var res = UniquePathsWithObstacles(obstacleGrid);
Console.WriteLine(res); Console.ReadKey();
} private static int UniquePathsWithObstacles(int[,] obstacleGrid) {
var m = obstacleGrid.GetLength(0);
var n = obstacleGrid.GetLength(1); if(m == 0 || n == 0) return 0;
var dp = new int[m, n]; for(var i = 0; i < m; i++) {
for(var j = 0; j < n; j++) {
if(obstacleGrid[i, j] == 0) {
if(i == 0 && j == 0) dp[i, j] = 1;
else if(i == 0) dp[i, j] = dp[i, j - 1];
else if(j == 0) dp[i, j] = dp[i - 1, j];
else dp[i, j] = dp[i, j - 1] + dp[i - 1, j];
}
}
}
return dp[m - 1, n - 1];
} }

以上给出1种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3682 访问。

2

分析

显而易见, 以上算法的时间复杂度为:O(m∗n)O(m*n)O(m∗n) 。

C#LeetCode刷题之#63-不同路径 II​​​​​​​(Unique Paths II)的更多相关文章

  1. C#LeetCode刷题之#62-不同路径(Unique Paths)

    目录 问题 示例 分析 问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3680 访问. 一个机器人位于一个 m x ...

  2. [Swift]LeetCode63. 不同路径 II | Unique Paths II

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  3. 【leetcode刷题笔记】Remove Duplicates from Sorted List II

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  4. 【leetcode刷题笔记】Search in Rotated Sorted Array II

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  5. 【leetcode刷题笔记】Remove Duplicates from Sorted Array II

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  6. 【leetcode刷题笔记】Binary Tree Level Order Traversal II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  7. C#LeetCode刷题-数组

    数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...

  8. LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]

    唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...

  9. LEETCODE —— Unique Paths II [Dynamic Programming]

    唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...

随机推荐

  1. Cyber Security - Palo Alto Firewall Security Zones

    Firewall Security Zones Zones: The foundational aspect of every Firewall. Police network traffic Enf ...

  2. 计算思维(Computational Thinking)在少儿编程中的体现

    本文主要针对少儿编程从业人员及正在学习编程的学生家长 大家好,我是C大叔,国内早期的少儿编程从业人员.一直以来都是在做scratch,JavaScript,python以及信息学奥赛C++的讲师,教研 ...

  3. 记IntelliJ IDEA创建spring mvc一次坑爹的操作!!!!

    本人刚开始学习spring mvc,遇到一问题,现在分享一下. 点击Next,创建项目完成,你会发现缺少很多东西. lib文件没有,里面的jar更没有.applicationContext.xml和d ...

  4. python中if及if-else如何使用

    if 结构 if 结构允许程序做出选择,并根据不同的情况执行不同的操作 基本用法 比较运算符 根据 PEP 8 标准,比较运算符两侧应该各有一个空格,比如:5 == 3. PEP8 标准 ==(相等) ...

  5. 好用到飞起的12个jupyter lab插件

    1 简介 jupyter lab作为jupyter notebook的升级改造版,除了更加人性化的交互界面以及更多的用户自主定制功能之外,最吸引人的就是其丰富多样的拓展插件,使得每个使用jupyter ...

  6. w10查看wifi密码

    1.选择网络和Internet设置 右键单击网络连接图标,选择“打开网络和Internet设置”. 2.选择网络和共享中心

  7. parted分区

    ((parted)mklabel ----创建磁盘标签New disk labeltype? gpt ---输入磁盘表情名(parted) p ----再次列出磁盘分区 (parted) mkpart ...

  8. Java Script 数组

    数组:有许多变量的集合,它们的名称和数据类型都是一致的. 定义    操作(添加修改) Var arr=new Array(): Var arr=[ ]; //定义 Var arr1=[ 1,2,3, ...

  9. Window版本的Python安装库大全

    1. 位置 python的pip安装包网站 https://www.lfd.uci.edu/~gohlke/pythonlibs/ 下载方法 wget https://download.lfd.uci ...

  10. Linux重定向用法详解

    大家好,我是良许. 相信大家平时都会有需要复制粘贴数据的时候,如果是打开文件进行复制粘贴,就不可避免的需要较多的鼠标与键盘的操作,就会比较繁琐.那么有没有可以省掉这些繁琐操作的复制粘贴的方法呢? 答案 ...