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. JDK12----------java环境变量配置

    https://jingyan.baidu.com/article/e5c39bf511bb3939d6603370.html

  2. Web.Config加密与解密

    可以使用受保护配置来加密 Web 应用程序配置文件(如 Web.config 文件)中的敏感信息(包括用户名和密码.数据库连接字符串和加密密钥).对配置信息进行加密后,即使攻击者获取了对配置文件的访问 ...

  3. Java Spring Boot VS .NetCore (三)Ioc容器处理

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  4. react 20180504

    react 入门 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  5. SA:利用SA算法解决TSP(数据是14个虚拟城市的横纵坐标)问题——Jason niu

    %SA:利用SA算法解决TSP(数据是14个虚拟城市的横纵坐标)问题——Jason niu X = [16.4700 96.1000 16.4700 94.4400 20.0900 92.5400 2 ...

  6. NEO智能合约开发(二)再续不可能的任务

      NEO智能合约开发中,应用合约比较简单,是的你没看错,应用合约比较简单. 应用合约三部曲,发布.调用.看结果.除了看结果工具比较缺乏,发布调用neogui最起码可以支撑你测试.   鉴权合约比较麻 ...

  7. VS2015编译FFMPEG,修改FFmpeg缓冲区大小解决实时流解码丢包问题,FFmpeg错误rtsp流地址卡死的问题,设置超时

    之前尝试过很多网上利用Windows编译FFmpeg的文章,都没有办法编译X64位的FFmpeg,有些教程中有专门提到编译64位的FFmpeg需要下载mingw-w64-install,但是编译的过程 ...

  8. Egret的Shape

    class ShapeTest extends egret.DisplayObjectContainer { public constructor() { super(); this.addEvent ...

  9. IDEA_Springboot启动Tomcat报错_APR

    使用idea新建一个Springboot项目 环境:idea+jdk8 启动异常如下: An older version [1.2.14] of the APR based Apache Tomcat ...

  10. 请求库之selenium模块

    本片导航: 介绍及安装 基本使用 选择器 等待元素被加载 元素交互操作 其他及练习   一.介绍 1.简单概述 selenium最初是一个自动化测试工具,而爬虫中使用它主要是为了解决requests无 ...