博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode Unique Paths II
阅读量:4183 次
发布时间:2019-05-26

本文共 1284 字,大约阅读时间需要 4 分钟。

Unique Paths II

 
Total Accepted: 2092 
Total Submissions: 8049

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.

Note: m and n will be at most 100.

I actually submitted the code with TLE using DFS. DP is the right solution:

class Solution { public:  int uniquePathsWithObstacles(vector
> &obstacleGrid) { int m = obstacleGrid.size(), n = m == 0 ? 0 : obstacleGrid[0].size(), count = 0; if (m == 0 || n == 0) return count; vector
> dp(m, vector
(n, 0)); int i, j; if (obstacleGrid[0][0] == 1) return 0; else dp[0][0] = 1; for (j = 1; j < n; ++j) dp[0][j] = dp[0][j - 1] & (obstacleGrid[0][j] == 0); for (i = 1; i < m; ++i) dp[i][0] = dp[i - 1][0] & (obstacleGrid[i][0] == 0); for (i = 1; i < m; ++i) for (j = 1; j < n; ++j) if (obstacleGrid[i][j] == 0) dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; else dp[i][j] = 0; return dp[m - 1][n - 1]; }};

转载地址:http://nrfoi.baihongyu.com/

你可能感兴趣的文章
(五)Git--分支管理
查看>>
(四)Git--远程仓库
查看>>
(六) Git--标签管理
查看>>
java中继承,子类是否继承父类的构造函数
查看>>
什么是Spring Cloud ?
查看>>
Qt下D-Bus的具体运用(软键盘输入法的实现)
查看>>
嵌入式环境的搭建(用于Arm开发板)
查看>>
Qt中文件读取的几种方式
查看>>
pyqt实现界面化编程
查看>>
qt写DLL文件并调用和出现的问题分析
查看>>
工厂模式(Factory)-设计模式(一)
查看>>
建造者模式(Builder)-设计模式(三)
查看>>
Qt 怎么给QWidget添加滚动条
查看>>
双十一冲刺业绩,完不成杀运营祭天?程序员:你们也有今天
查看>>
搜狗输入法到底算不算恶意挟持百度搜索流量?五个测试告诉你答案
查看>>
百度成为美国领先的人工智能联盟的第一个中国成员
查看>>
程序员资讯:QR代码在公共交通中得到越来越多的采用
查看>>
当了将近十年的程序员,为什么从来没见过程序员带孩子
查看>>
程序员面试中最容易碰到的五个套路!应届生最容易上当
查看>>
三种不同的程序员,你属于哪一种?如果要裁员,你会让谁走?
查看>>