[LeetCode] 63. Unique Paths II_ Medium tag: Dynamic Programming
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.
Example 1:
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 这个题目思路还是跟[LeetCode] 62. Unique Paths_ Medium tag: Dynamic Programming 类似,只不过在初始化和计算时要考虑是否为obstacle即可。 Code
class Solution:
def uniquePath(self, nums):
if not nums or len(nums[0]) == 0:
return 0
lr, lc = len(nums), len(nums[0])
mem = [[0] * lc for _ in range(lr)]
for i in range(lr):
if nums[i][0] != 1:
mem[i][0] = 1
else:
break
for i in range(lc):
if nums[0][i] != 1:
mem[0][i] = 1
else:
break
for i in range(1, lr):
for j in range(1, lc):
mem[i][j] = 0 if nums[i][j] == 1 else mem[i - 1][j] + mem[i][j - 1]
return mem[lr - 1][lc - 1]
[LeetCode] 63. Unique Paths II_ Medium tag: Dynamic Programming的更多相关文章
- [LeetCode] 45. Jump Game II_ Hard tag: Dynamic Programming
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] 132. Palindrome Partitioning II_ Hard tag: Dynamic Programming
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [leetcode] 63. Unique Paths II (medium)
原题 思路: 用到dp的思想,到row,col点路径数量 : path[row][col]=path[row][col-1]+path[row-1][col]; 遍历row*col,如果map[row ...
- [LeetCode] 221. Maximal Square _ Medium Tag: Dynamic Programming
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and re ...
- [LeetCode] 139. Word Break_ Medium tag: Dynamic Programming
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- LeetCode 63. Unique Paths II不同路径 II (C++/Java)
题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
- [LeetCode] 62. Unique Paths_ Medium tag: Dynamic Programming
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- [LeetCode] 63. 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 63. Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
随机推荐
- Angular路由——子路由
一.子路由语法: 二.实例 在商品详情页面,除了显示商品id信息,还显示了商品描述,和销售员的信息. 通过子路由实现商品描述组件和销售员信息组件展示在商品详情组件内部. 1.新建2个组件修改其内容 n ...
- codeforces 493 div1 e
题解: 和这件zhcs的那题有点像 第一种做法是考虑i,i+1之间的贡献 这样子就是矩形加减然后求矩形最小值个数 另一种做法是我们从左向右维护mx-nx-r+l 跟之前那题一样我们知道这个的最小值为0 ...
- C++运算符重载——输入/输出运算符
为了与IO标准库一致,重载输入输出运算符函数的第一个行参应该是流的引用,第二个行参是对象的引用. 如果重载为类的成员函数,第一个行参应该是对象的引用,第二个行参是流的引用. 使用方式是 ClassOb ...
- SSH项目需要的所有架包
原地址:https://blog.csdn.net/qq_35816104/article/details/54346182 SSH框架:struts2 hibernate spring 该三大框 ...
- RPC远程调用——Dubbo
1.安装Zookeeper a.下载Zookeeper后解压 b.进入根目录下的conf文件夹,将zoo_sample.cfg改成bak文件,并复制一个修改为zoo.cfg,修改相关配置 # The ...
- C 小白的 thrift 环境搭建
公司有个通讯 是用的 thrift ,thrift 是个什么都东西,可以类比 webservice 吧,比 webservice 高效些,不管是啥,搞他! 先在 mac 上搞本地开发环境 网上一搜 貌 ...
- 最近公共祖先(LCA)的三种求解方法
转载来自:https://blog.andrewei.info/2015/10/08/e6-9c-80-e8-bf-91-e5-85-ac-e5-85-b1-e7-a5-96-e5-85-88lca- ...
- Python编程中出现ImportError: bad magic number in 'numpy': b'\x03\xf3\r\n'
在终端输入ls -a 会出现一个.pyc的文件,将文件删掉
- EasyUI 分页 偶遇 问题
当 存在大量 重复 数据字段的 时候 entity.AsNoTracking().ToList().Skip((page.pageNumber - 1) * page.rows).Take(page. ...
- EMI优化
一般印刷电路板之间的高速信号线路无法通过FCC和VDE辐射测试. 优化方案有以下3种: 1.高频滤波 通常做法在每个逻辑驱动器上串联一个小阻抗,并经过一个旁路电容接地. 旁路电容接地需足够干净,如机箱 ...