LeetCode - 63. 不同路径II(动态规划、递归)

  1. 不同路径II

这个是对上一个进行了障碍物的判断,增加了中间动态规划的判断条件,实质上是不变的

题目:
一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” )。

机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。

现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径?

在这里插入图片描述
网格中的障碍物和空位置分别用 1 和 0 来表示。

在这里插入图片描述

输入:obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]]
输出:2
解释:
3x3 网格的正中间有一个障碍物。
从左上角到右下角一共有 2 条不同的路径:
1. 向右 -> 向右 -> 向下 -> 向下
2. 向下 -> 向下 -> 向右 -> 向右

示例2:
在这里插入图片描述

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

思路:
在这里插入图片描述

int m = obstacleGrid.length, n = obstacleGrid[0].length;
int[][] dp = new int[m][n];
for (int i = 0; i < m && obstacleGrid[i][0] == 0; i++) {
    dp[i][0] = 1;
}
for (int j = 0; j < n && obstacleGrid[0][j] == 0; j++) {
    dp[0][j] = 1;
}


方法一:动态规划

class Solution {
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        int n = obstacleGrid.length, m = obstacleGrid[0].length;
        int[] f = new int[m];

        f[0] = obstacleGrid[0][0] == 0 ? 1 : 0;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                if (obstacleGrid[i][j] == 1) {
                    f[j] = 0;
                    continue;
                }
                if (j - 1 >= 0 && obstacleGrid[i][j - 1] == 0) {
                    f[j] += f[j - 1];
                }
            }
        }
        
        return f[m - 1];
    }
}


方法二:递归

这个递归没有把重复的步数储存在数组中,会导致超时

    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        return (int) helper(obstacleGrid, 0, 0);
    }

    public static long helper(int[][] obstacleGrid, int down, int right) {
        if (obstacleGrid[down][right] == 1)
            return 0;
        if (right == obstacleGrid[0].length - 1 && down == obstacleGrid.length - 1) {
            if (obstacleGrid[down][right] == 1)
                return 0;
            else
                return 1;
        }
        if (right == obstacleGrid[0].length - 1 || down == obstacleGrid.length - 1) {
            if (right == obstacleGrid[0].length - 1)
                return helper(obstacleGrid, down + 1, right);
            return helper(obstacleGrid, down, right + 1);
        }
        return helper(obstacleGrid, down, right + 1) + helper(obstacleGrid, down + 1, right);
    }


递归包含大量的重复计算,所以就把计算过的值保存在map中

    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        return helper(obstacleGrid, 0, 0, new HashMap<String, Integer>());
    }

    public static int helper(int[][] obstacleGrid, int down, int right, Map<String, Integer> map) {
        String key = down + "and" + right;
        int result = 0;
        if (map.containsKey(key))
            return map.get(key);
        if (obstacleGrid[down][right] == 1) {
            result = 0;
            map.put(key, result);
            return result;
        }
        if (right == obstacleGrid[0].length - 1 && down == obstacleGrid.length - 1) {
            if (obstacleGrid[down][right] == 1) {
                result = 0;
            } else {
                result = 1;
            }
            map.put(key, result);
            return result;
        }
        if (right == obstacleGrid[0].length - 1 || down == obstacleGrid.length - 1) {
            if (right == obstacleGrid[0].length - 1) {
                result = helper(obstacleGrid, down + 1, right, map);
            } else {
                result = helper(obstacleGrid, down, right + 1, map);
            }
            map.put(key, result);
            return result;
        }
        result = helper(obstacleGrid, down, right + 1, map) + helper(obstacleGrid, down + 1, right, map);
        map.put(key, result);
        return result;
    }


posted @ 2020-11-11 23:21  your_棒棒糖  阅读(26)  评论(0编辑  收藏  举报