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 ...
随机推荐
- Azure 提供负载均衡(一)Azure Traffic Manager 为我们的Web项目提供负载均衡
一,引言 上一篇讲到我们将自己的Net Core Web 项目部署到 Azure 的 Web App 的一项 pass 服务,假如随着项目的日益增长的访问量,之前部署到单节点的应用可能无法保证其稳定性 ...
- layui 魔改:上传时的真实进度条
这个问题本身不复杂,难点在于需要改 layui 的源码. HTML略. 网页的JS域: layui.use(['upload','element','layer'], function(){ var ...
- Repeating Decimals UVA - 202---求循环部分
原题链接:https://vjudge.net/problem/UVA-202 题意:求一个数除以一个数商,如果有重复的数字(循环小数),输出,如果没有,输出前50位. 题解:这个题一开始考虑的是一个 ...
- 推荐一款技术人必备的接口测试神器:Apifox
1. 背景 作为互联网行业技术从业者,接口调试是必不可少的一项技能,通常我们都会选择使用 Postman 这类工具来进行接口调试,在接口调试方面 Postman 做的确实非常出色.当然除了Postma ...
- LQB201808全球变暖 bfs
#include<iostream> #include<stdio.h> #include<stdlib.h> #include<time.h> #in ...
- c语言大小写转化函数(包括字母和字符串)
本憨憨忘了好几次了,这次一定记住他们! 首先大小写相差32.转换的话自己写函数也是可以写出来的. 1.字母 如果是字母转的话,用toupper(),tolower() 头文件是<ctype.h& ...
- MacOS下SpringBoot基础学习
学于黑马和传智播客联合做的教学项目 感谢 黑马官网 传智播客官网 微信搜索"艺术行者",关注并回复关键词"springboot"获取视频和教程资料! b站在线视 ...
- UDP 网络程序-发送_接收数据
""" 创建udp连接 发送数据给 """ from socket import * # 创建udp套接字,使用SOCK_DGRAM udp ...
- HTML <hr> 标签
高佣联盟 www.cgewang.com HTML <hr> 标签 实例 当内容的主题发生变化时,使用 <hr> 标签进行分隔: <h1>HTML</h1&g ...
- C/C++编程笔记:C语言写推箱子小游戏,大一学习C语言练手项目
C语言,作为大多数人的第一门编程语言,重要性不言而喻,很多编程习惯,逻辑方式在此时就已经形成了.这个是我在大一学习 C语言 后写的推箱子小游戏,自己的逻辑能力得到了提升,在这里同大家分享这个推箱子小游 ...