63.Unique-Paths-II

63. Unique Paths II

题目地址

https://leetcode.com/problems/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 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

代码

Approach #1 Dyanmic Programming

class Solution {
  public int uniquePathsWithObstacles(int[][] obstacleGrid) {
    int C = obstacleGrid[0].length;
    int[] dp = new int[C];
    dp[0] = 1;
    for (int[] row : obstacleGrid) {
      for (int col = 0; col < C; col++) {
        if (row[col] == 1) {
          dp[col] = 0;
        } else if (col >= 1) { // 防止越界
          dp[col] += dp[col - 1];
        }
      }
    }
    return dp[C - 1];
  }
}

#2

class Solution {
  public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        int R = obstacleGrid.length;
    int C = obstacleGrid[0].length;

    if (obstacleGrid[0][0] == 1)        return 0;

    obstacleGrid[0][0] = 1;

    for (int i = 1; i < R; i++) {
      obstacleGrid[i][0] = (obstacleGrid[i][0] == 0 && obstacleGrid[i - 1][0] == 1) ? 1 : 0;
    }

    for (int i = 1; i < C; i++) {
      obstacleGrid[0][i] = (obstacleGrid[0][i] == 0 && obstacleGrid[0][i - 1] == 1) ? 1 : 0; 
    }

    for (int i = 1; i < R; i++) {
      for (int j = 1; j < C; j++) {
        if (obstacleGrid[i][j] == 0) {
          obstacleGrid[i][j] = obstacleGrid[i - 1][j] + obstacleGrid[i][j - 1];
        } else {
          obstacleGrid[i][j] = 0;
        }
      }
    }

    return obstacleGrid[R - 1][C - 1];
  }
}

#3

class Solution {
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        int R = obstacleGrid.length;
        int C = obstacleGrid[0].length;
        int[][] dp = new int[R][C];
        dp[0][0] = 1;

        for (int i = 1; i < R; i++) {
            dp[i][0] = (obstacleGrid[i][0] == 0 && obstacleGrid[i - 1][0] == 0) ? 1 : 0;
        }

        for (int i = 1; i < C; i++) {
            dp[0][i] = (obstacleGrid[0][i] == 0 && obstacleGrid[0][i - 1] == 0) ? 1 : 0; 
        }

        for (int row = 0; row < R; row++) {
            for (int col = 0; col < C; col++) {
                if (obstacleGrid[row][col] == 1) {
                    dp[row][col] = 0;
                } else if (col >= 1 && row >= 1) { 
                    dp[row][col] = dp[row][col - 1] +  dp[row - 1][col];
                }
            }
        }
        return dp[R - 1][C - 1];
    }
}

Last updated