Algorithm leetcode|63. Different paths II (rust punches hard)



63. Different paths II:

A robot is located in m x nthe upper left corner of a grid (the starting point is labeled "Start" in the figure below).

The robot can only move one step down or to the right at a time. The robot tries to reach the bottom right corner of the grid (marked "Finish" in the image below).

Now consider that there are obstacles in the grid. So how many different paths will there be from top left to bottom right?

Obstacles and empty positions in the grid are denoted by 1and , respectively 0.

Example 1:

输入:
	
	obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]]
	
输出:
	
	2
	
解释:

	3x3 网格的正中间有一个障碍物。
	从左上角到右下角一共有 2 条不同的路径:
	1. 向右 -> 向右 -> 向下 -> 向下
	2. 向下 -> 向下 -> 向右 -> 向右

Example 2:

输入:
	
	obstacleGrid = [[0,1],[0,0]]
	
输出:
	
	1

hint:

  • m == obstacleGrid.length
  • n == obstacleGrid[i].length
  • 1 <= m, n <= 100
  • obstacleGrid[i][j] is 0 or 1

analyze:

  • Facing this algorithm problem, the second leader fell into deep thought again.
  • This question is similar to 62. Different paths , but because there are obstacles in random positions, it cannot be solved directly by mathematical combination numbers.
  • Then dynamic programming becomes the first choice at this time. The number of distinct paths to each point is the sum of the number of distinct paths to the point above and the number of distinct paths to the left. We can process it from top to bottom by row (it is also possible to process it from left to right by column, but it is a bit awkward, because the first dimension of a two-dimensional array is a row, and the second dimension is a column. It is customary to use rows Processing), it will be found that each line is only related to the result of the previous line, so there is no need m * nfor additional space, but only mspace, that is, the idea of ​​​​rolling the array.

answer:

rust:

impl Solution {
    
    
    pub fn unique_paths_with_obstacles(obstacle_grid: Vec<Vec<i32>>) -> i32 {
    
    
        let rows = obstacle_grid.len();
        let cols = obstacle_grid[0].len();
        let mut dp = vec![0; cols];

        if obstacle_grid[0][0] == 0 {
    
    
            dp[0] = 1;
        }

        (0..rows).for_each(|r| {
    
    
            (0..cols).for_each(|c| {
    
    
                if obstacle_grid[r][c] == 1 {
    
    
                    dp[c] = 0;
                } else if c > 0 && obstacle_grid[r][c - 1] == 0 {
    
    
                    dp[c] += dp[c - 1];
                }
            });
        });

        return dp[cols - 1];
    }
}

go:

func uniquePathsWithObstacles(obstacleGrid [][]int) int {
    
    
    rows, cols := len(obstacleGrid), len(obstacleGrid[0])
	dp := make([]int, cols)

	if obstacleGrid[0][0] == 0 {
    
    
		dp[0] = 1
	}

	for r := 0; r < rows; r++ {
    
    
		for c := 0; c < cols; c++ {
    
    
			if obstacleGrid[r][c] == 1 {
    
    
				dp[c] = 0
				continue
			}
			if c > 0 && obstacleGrid[r][c-1] == 0 {
    
    
				dp[c] += dp[c-1]
			}
		}
	}

	return dp[cols-1]
}

c++:

class Solution {
    
    
public:
    int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
    
    
        const int rows = obstacleGrid.size(), cols = obstacleGrid.at(0).size();
        vector<int> dp(cols);

        dp[0] = (obstacleGrid[0][0] == 0);
        for (int r = 0; r < rows; ++r) {
    
    
            for (int c = 0; c < cols; ++c) {
    
    
                if (obstacleGrid[r][c] == 1) {
    
    
                    dp[c] = 0;
                    continue;
                }
                if (c > 0 && obstacleGrid[r][c - 1] == 0) {
    
    
                    dp[c] += dp[c - 1];
                }
            }
        }

        return dp.back();
    }
};

python:

class Solution:
    def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
        rows, cols = len(obstacleGrid), len(obstacleGrid[0])
        dp = [0] * cols
        if obstacleGrid[0][0] == 0:
            dp[0] = 1
        for r in range(rows):
            for c in range(cols):
                if obstacleGrid[r][c] == 1:
                    dp[c] = 0
                    continue
                if c > 0 and obstacleGrid[r][c - 1] == 0:
                    dp[c] += dp[c - 1]
        return dp[cols - 1]


java:

class Solution {
    
    
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
    
    
        final int   rows = obstacleGrid.length, cols = obstacleGrid[0].length;
        final int[] dp   = new int[cols];

        if (obstacleGrid[0][0] == 0) {
    
    
            dp[0] = 1;
        }

        for (int i = 0; i < rows; ++i) {
    
    
            for (int j = 0; j < cols; ++j) {
    
    
                if (obstacleGrid[i][j] == 1) {
    
    
                    dp[j] = 0;
                    continue;
                }
                if (j > 0 && obstacleGrid[i][j - 1] == 0) {
    
    
                    dp[j] += dp[j - 1];
                }
            }
        }

        return dp[cols - 1];
    }
}

Thank you very much for reading this article~
Welcome to [Like][Favorite][Comment] Go three times in a row~It’s
not difficult to give up, but it must be cool~
I hope we all can improve a little every day~
This article is written by the white hat of the second master: https://le-yi.blog.csdn.net/Blog original~


Guess you like

Origin blog.csdn.net/leyi520/article/details/131824516