leetcode Unique Paths II

原创 2013年12月03日 21:03:10

Unique Paths II

 Total Accepted: 2092 Total Submissions: 8049My Submissions

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<vector<int> > &obstacleGrid) {
    int m = obstacleGrid.size(), n = m == 0 ? 0 : obstacleGrid[0].size(), count = 0;
    if (m == 0 || n == 0)
      return count;
    vector<vector<int>> dp(m, vector<int>(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];

  }
};


LeetCode 63 — Unique Paths II(C++ Java Python)

题目:http://oj.leetcode.com/problems/unique-paths-ii/ Follow up for "Unique Paths": Now consider if so...
  • dragon_dream
  • dragon_dream
  • 2014年02月25日 22:19
  • 2068

Unique Paths II -- LeetCode

原题链接: http://oj.leetcode.com/problems/unique-paths-ii/  这道题跟Unique Paths非常类似,只是这道题给机器人加了障碍,不是每次都有两个...
  • linhuanmars
  • linhuanmars
  • 2014年03月26日 04:15
  • 10712

LeetCode63——Unique Paths II

LeetCode63——Unique Paths II 跟LeetCode62——Unique Paths不同的是,这里是要在路径上设置障碍的。 因此不能用组合的方法去做了(也许可以,但我实在想不到)...
  • zhangxiao93
  • zhangxiao93
  • 2015年11月02日 10:01
  • 355

【LeetCode】Unique Paths 解题报告

【题目】 A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram belo...
  • ljiabin
  • ljiabin
  • 2014年12月08日 17:19
  • 4063

LeetCode 62 — Unique Paths(C++ Java Python)

题目:http://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corner of a m ...
  • dragon_dream
  • dragon_dream
  • 2014年02月23日 22:00
  • 2458

[LeetCode][Java] Unique Paths II

题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How...
  • Evan123mg
  • Evan123mg
  • 2015年07月17日 09:59
  • 750

LeetCode63:Unique Paths II

Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many uniqu...
  • u012501459
  • u012501459
  • 2015年06月12日 16:05
  • 878

63 Unique Paths II

题目链接:https://leetcode.com/problems/unique-paths-ii/题目:Follow up for "Unique Paths":Now consider if s...
  • ChilseaSai
  • ChilseaSai
  • 2015年10月17日 15:31
  • 674

[C++]LeetCode: 78 Unique Paths II

题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many ...
  • cinderella_niu
  • cinderella_niu
  • 2015年01月08日 13:08
  • 656

LeetCode-62-Unique Paths(动态规划)-Medium

题目理解: 在只允许从左向右,从上到下移动的情况下,从mxn的矩阵(0,0)点,到(m-1,n-1)点,有多少路径; 题目分析: 1. 回溯遍历会超时; 2. 动规; 解题代...
  • eddy_liu
  • eddy_liu
  • 2016年03月14日 08:51
  • 1101
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode Unique Paths II
举报原因:
原因补充:

(最多只允许输入30个字)