[leetcode DP]63. Unique Paths II
Follow up for "Unique Paths":
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.
For example,
There is one obstacle in the middle of a 3x3 grid as illustrated below.
[
[0,0,0],
[0,1,0],
[0,0,0]
]
The total number of unique paths is 2.
障碍物对应的dp表中设为0即可,为了使代码简洁,多加了两行dp[i][0]和dp[0][j]
class Solution(object):
def uniquePathsWithObstacles(self, obstacleGrid):
m,n = len(obstacleGrid),len(obstacleGrid[0])
dp = [[0 for j in range(n+1)] for i in range(m+1)]
dp[0][1] = 1
for i in range(1,m+1):
for j in range(1,n+1):
if not obstacleGrid[i-1][j-1]:
dp[i][j] = dp[i][j-1] + dp[i-1][j]
return dp[m][n]
还有一种占用O(N)空间的方法,感觉挺不错的
思路:用一个数组tmp记录每一行中每一个位置拥有的次数,每到一个没有障碍物的位置,那么这个位置自动继承上一个位置上所拥有的次数
class Solution(object):
def uniquePathsWithObstacles(self, obstacleGrid):
m,n=len(obstacleGrid),len(obstacleGrid[0])
tmp = [0]*(n+1)
tmp [n-1] = 1
for i in range(m-1,-1,-1):
for j in range(n-1,-1,-1):
if obstacleGrid[i][j]:
tmp[j] = 0
else:
tmp[j] += tmp [j+1]
return tmp[0]
[leetcode DP]63. Unique Paths II的更多相关文章
- 【一天一道LeetCode】#63. Unique Paths II
一天一道LeetCode (一)题目 Follow up for "Unique Paths": Now consider if some obstacles are added ...
- 【LeetCode】63. Unique Paths II
Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...
- 【LeetCode】63. Unique Paths II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- LeetCode OJ 63. Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- leetcode@ [62/63] Unique Paths II
class Solution { public: int uniquePathsWithObstacles(vector<vector<int>>& obstacleG ...
- leetcode 62. Unique Paths 、63. Unique Paths II
62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...
- 62. Unique Paths && 63 Unique Paths II
https://leetcode.com/problems/unique-paths/ 这道题,不利用动态规划基本上规模变大会运行超时,下面自己写得这段代码,直接暴力破解,只能应付小规模的情形,当23 ...
- 【LeetCode练习题】Unique Paths II
Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...
- [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 简单】 第六十七题 回文链表
请判断一个链表是否为回文链表. 示例 1: 输入: 1->2 输出: false 示例 2: 输入: 1->2->2->1 输出: true 进阶: 你能否用 O(n) 时间复 ...
- sklearn_PCA主成分降维
# coding:utf-8 import pandas as pd import numpy as np from pandas import Series,DataFramefrom sklear ...
- Python练习-三级菜单与"片儿"无关!
# 编辑者:闫龙 #三级目录 menu = { '北京':{ '海淀':{ '五道口':{'soho':{},'网易':{},'google':{}}, '中关村':{'爱奇艺':{},'汽车之家': ...
- Bresenham直线算法与画圆算法
在我们内部开发使用的一个工具中,我们需要几乎从 0 开始实现一个高效的二维图像渲染引擎.比较幸运的是,我们只需要画直线.圆以及矩形,其中比较复杂的是画直线和圆.画直线和圆已经有非常多的成熟的算法了,我 ...
- 服务发现 consul cluster 的搭建
consul cluster setup 介绍和指南: consul用于服务发现.当底层服务发生变化时,能及时更新正确的mysql服务IP. 并提供给业务查询.但需要自行编写脚本,监测数据库状态和切断 ...
- 全面了解Nginx主要应用场景【转】
前言 本文只针对 Nginx 在不加载第三方模块的情况能处理哪些事情,由于第三方模块太多所以也介绍不完,当然本文本身也可能介绍的不完整,毕竟只是我个人使用过和了解到过得.所以还请见谅,同时欢迎留言交流 ...
- Spring入门实例
Spring 是一个开源框架,是为了解决企业应用程序开发复杂性而创建的.框架的主要优势之一就是其分层架构,分层架构允许您选择使用哪一个组件,同时为 J2EE 应用程序开发提供集成的框架. 控制反转:应 ...
- 查找Python包的依赖包(语句)
Window 10家庭中文版,Python 3.6.4, 今天看完了urllib3的官文(官方文档),因为没有具体使用过,所以,仍然是一知半解,但是,突然想知道 urllib3以及前面学习过的requ ...
- Python2和Python3同时安装到Windows
上月已经把Python2安装好了,安装目录和及其下的Scripts也在安装时添加到了环境变量PATH中,可以使用python命令执行程序. 安装包:python-2.7.14.amd64.msi(没有 ...
- mysql命令gruop by报错this is incompatible with sql_mode=only_full_group_by
在mysql 工具 搜索或者插入数据时报下面错误: ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause ...