C#LeetCode刷题之#63-不同路径 II(Unique Paths II)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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 条不同的路径:
- 向右 -> 向右 -> 向下 -> 向下
- 向下 -> 向下 -> 向右 -> 向右
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:
- Right -> Right -> Down -> Down
- 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)的更多相关文章
- C#LeetCode刷题之#62-不同路径(Unique Paths)
目录 问题 示例 分析 问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3680 访问. 一个机器人位于一个 m x ...
- [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 ...
- 【leetcode刷题笔记】Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- 【leetcode刷题笔记】Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- 【leetcode刷题笔记】Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- 【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 ...
- C#LeetCode刷题-数组
数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...
- LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...
- LEETCODE —— Unique Paths II [Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...
随机推荐
- Illegal instant due to time zone offset transition (Asia/Shanghai)_夏令时问题
项目报错信息: Connot parse "1991-04-14",illegal instant due to time zone offset transition(Asia/ ...
- 评测Loki日志工具
评测Loki日志工具 目录 评测Loki日志工具 部署Loki 配置grafana 总结: 优势: 劣势: 本文仅对Loki进行简单评测,不涉及原理和细节. 部署Loki Loki是grafana团队 ...
- 一款直击痛点的优秀http框架,让我超高效率完成了和第三方接口的对接
1.背景 因为业务关系,要和许多不同第三方公司进行对接.这些服务商都提供基于http的api.但是每家公司提供api具体细节差别很大.有的基于RESTFUL规范,有的基于传统的http规范:有的需要在 ...
- 修改map中原来的各种Key
简单描述: 做数据迁移的时候,需要展示数据库的字段信息,但是我发现 Oracle的sql查询到的结果 出来默认是大写的 和 前端vue的参数小写开头+驼峰 不太一样 所以后台取到的数据都是是引用Lis ...
- Python语言及其应用|PDF高清完整版免费下载|百度云盘|Python
百度云盘:Python语言及其应用PDF高清完整版免费下载 提取码:6or6 内容简介 本书介绍Python 语言的基础知识及其在各个领域的具体应用,基于最新版本3.x.书中首先介绍了Python 语 ...
- 分布式系统中幂等性、at least once 和 at most once 问题
讨论一下分布式系统传输过程中常见的at least once 还是 at most once 问题.一般在一次传输过程中,失败与否是使用最大等待时间(记为time out)来判断是否传输成功,如果超过 ...
- 看完这一篇,再也不怕面试官问到IntentService的原理
IntentService是什么 在内部封装了 Handler.消息队列的一个Service子类,适合在后台执行一系列串行依次执行的耗时异步任务,方便了我们的日常coding(普通的Service则是 ...
- Day12_搜索过滤
学于黑马和传智播客联合做的教学项目 感谢 黑马官网 传智播客官网 微信搜索"艺术行者",关注并回复关键词"乐优商城"获取视频和教程资料! b站在线视频 0.学习 ...
- 找工作的你不容错过的45个PHP面试题附答案(上篇)
Q1: == 和 === 之间有什么区别? 如果是两个不同的类型,运算符 == 则在两个不同的类型之间进行强制转换 === 操作符执行 ’ 类型安全比较’ 这意味着只有当两个操作数具有相同的类型和相同 ...
- Python匿名函数_return语句
Python匿名函数: 使用 lambda 关键字创建匿名函数: lambda 定义的函数只是一个表达式,而不是代码块 lambda 函数拥有自己的命名空间,不能够访问参数列表之外的 或 全局命名空间 ...