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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [leetcode] 63. Unique Paths II (medium)

    原题 思路: 用到dp的思想,到row,col点路径数量 : path[row][col]=path[row][col-1]+path[row-1][col]; 遍历row*col,如果map[row ...

  4. [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 ...

  5. [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 ...

  6. 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). ...

  7. [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 ...

  8. [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 ...

  9. leetcode 63. Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

随机推荐

  1. numpy的array合并-【老鱼学numpy】

    概述 本节主要讲述如何把两个数组按照行或列进行合并. 按行进行上下合并 例如: import numpy as np a = np.array([1, 1, 1]) b = np.array([2, ...

  2. Codeforces 730 J.Bottles (01背包)

    <题目链接> 题目大意: 有n个瓶子,各有水量和容量.现在要将这写瓶子里的水存入最少的瓶子里.问你最少需要的瓶子数?在保证瓶子数最少的情况下,要求转移的水量最少. 解题分析:首先,最少的瓶 ...

  3. elk安装&集群配置

    ---恢复内容开始--- 这里我用以elasticsearch-5.3.2.kibana-5.3.0.logstash-5.3.0的版本为例: 1.创建elastic用户,这里elasticsearc ...

  4. JS对象与原型链

    每个函数都存在一个prototype的属性,然后这个属性值为一个对象,我们称之为原型对象 每个对象都存在着一个隐藏的属性"__proto__" 这个属性引用了创建这个对象的函数的p ...

  5. 用户管理和su,id 命令

    useradd userdel usermod groupadd groupdel 用户管理 为什么需要有用户? 1. linux是一个多用户系统 2. 权限管理(权限最小化) 用户:存在的目录是为了 ...

  6. angular笔记_10

    过滤器 currency:1:2:3                     与货币相关 第一个参数是符号 第二个参数是保留小数点几位 number:1                         ...

  7. DWM1000 测距原理简单分析 之 SS-TWR代码分析1 -- [蓝点无限]

    蓝点DWM1000 模块已经打样测试完毕,有兴趣的可以申请购买了,更多信息参见 蓝点论坛 正文: 这一篇内容主要是通过官方源码理解SS-TWR 细节 代码下载链接:https://download.c ...

  8. EasyUI 分页 偶遇 问题

    当 存在大量 重复 数据字段的 时候 entity.AsNoTracking().ToList().Skip((page.pageNumber - 1) * page.rows).Take(page. ...

  9. help文档制作 chm

    程序中的help文档制作 所用工具:HTML Help Workshop 文件包括:各个html文档,帮助页面的具体内容 hhc文档:help的目录文件 hhk文档:help的索引文件 MAP文件夹中 ...

  10. javascript的数组之map()

    map()方法创建一个新数组,其结果是该数组中的每个元素都调用一个提供的回调函数后返回的结果.新数组 // ES6 let numbers = [1, 5, 10, 15]; let doubles ...