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 ...
随机推荐
- 多国正在遭遇新型勒索病毒Petya侵袭
北京时间2017年6月27日晚,据外媒消息,多国正在遭遇 Petya 勒索病毒袭击,政府.银行.电力系统.通讯系统.企业以及机场都受到不同程度影响.请予关注,并做相应防范.相关事件描述及防范措施如下: ...
- UVA1104 芯片难题 Chips Challenge
题目链接 题意 网格上放点,有些强制放,有些不能放,有些可以放可以不放.要求: 第 \(i\) 行的点数 = 第 \(i\) 列的点数 每一行每一列的点数不超过总点数的 \(k\) 倍(\(k\) 已 ...
- centos7 安装 isign
centos应该自带python和openssl,这两个就不用装了, 先安装zip和git yum install -y unzip zip yum install git 然后克隆代码: https ...
- vue 修改路由
直接放代码: this.$router.push({ path: "/login" });
- Python 写入excel时的字体格式设置
转自:https://blog.csdn.net/kuangzhi9124/article/details/81940919 下面代码设置了单元格的字体.位置居中.框线,可以将格式调成自己需要的 im ...
- 自定义placeholder样式
::-webkit-input-placeholder { /* WebKit, Blink, Edge */ color: #909; } :-moz-placeholder { /* Mozill ...
- IDEA 修改快捷键和Myeclipse 快捷键一致
介绍 我们知道IDEA这款开发工具功能很强大,为了简化开发步骤,提高开发效率,使用快捷键很显然是必不可少的,那么怎么才能使得IDEA快捷键和MyEclipse快捷键 保持相同呢? 第一种方法,一个快捷 ...
- 动态规划入门(dp)
dp的基本思想,是把大问题转化成一个个小问题,然后递归解决. 所以本质思想的话还是递归. dp最重要的是要找到状态转移方程,也就是把大问题化解的过程. 举个例子 一个数字金字塔 在上面的数字三角形中寻 ...
- 本地建立mysql服务器
这里可以学到 : 搭建一个mysql的后台服务器,构建自己的本地数据库表,可以满足一些自己学习增删改查的简单需求 1.打开mysql官网 https://www.mysql.com/downloa ...
- Seaborn基础3
import seaborn as sns import numpy as np import matplotlib.pyplot as plt sns.set(rc = {"figure. ...