美文网首页
62&63&64. Unique Paths&a

62&63&64. Unique Paths&a

作者: exialym | 来源:发表于2016-11-08 14:17 被阅读22次

    Unique Paths

    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).
    How many possible unique paths are there?
    这是一个典型的DP问题
    假设只有一个格:

    走法是1
    ++
    2:1
    +++
    3:1
    ++
    ++
    4个排成方形就是2种
    +++
    +++
    +++
    这样的就是:
    1,1,1
    1,2,3
    1,3,6
    所以除了横着第一排和竖着第一排都是1种,其他的都是上边和左边的格的步数相加

    var uniquePaths = function(m, n) {
        if (m===0||n===0)
            return 0;
        var res = [];
        for (var i = 0;i < m;i++) {
            res.push([1]);
            for(var j = 1;j < n;j++) {
                if (i===0)
                    res[i].push(1);
                else 
                    res[i].push(res[i-1][j]+res[i][j-1]);
            }
        }
        return res.pop().pop();
    };
    

    Unique Paths II

    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.
    和上面一样的思路,遇到有阻碍的点,这个格子可能的路线数就置0,需要注意的是第一排和第一列,只要前面或上面有0,这个格子也是0。其余格子还是将左边的和上边的数加起来就好。

    var uniquePathsWithObstacles = function(obstacleGrid) {
        var m = obstacleGrid.length;
        var n = obstacleGrid[0].length;
        if (m===0||n===0)
            return 0;
        for (var i = 0;i < m;i++) {
            for(var j = 0;j < n;j++) {
                if (obstacleGrid[i][j]===1) {
                    obstacleGrid[i][j]=0;
                    continue;
                }
                if (i===0) {
                    if (obstacleGrid[i][j-1]===0) obstacleGrid[i][j]=0;
                    else obstacleGrid[i][j]=1;
                } else if (j===0) {
                    if (obstacleGrid[i-1][j]===0) obstacleGrid[i][j]=0;
                    else obstacleGrid[i][j]=1;
                }
                else 
                    obstacleGrid[i][j] = obstacleGrid[i-1][j]+obstacleGrid[i][j-1];
            }
        }
        return obstacleGrid.pop().pop();
    };
    

    Minimum Path Sum

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.
    Note: You can only move either down or right at any point in time.
    这道题和上面的思想很相似

    var minPathSum = function(grid) {
        var row = grid.length;
        var col = row===0 ? 0 : grid[0].length;
        for (var i = 0;i < row;i++) {
            if (i!==0)
                grid[i][0] = grid[i-1][0]+grid[i][0];
            for (var j = 1;j < col;j++) {
                if (i===0)
                    grid[0][j] = grid[0][j-1]+grid[0][j];
                else
                    grid[i][j] = Math.min(grid[i][j-1],grid[i-1][j]) + grid[i][j];
            }
        }
        return grid[row-1][col-1];
    };
    

    相关文章

      网友评论

          本文标题:62&63&64. Unique Paths&a

          本文链接:https://www.haomeiwen.com/subject/wavauttx.html