LeetCode刷题 Day39

68 阅读2分钟

LeetCode刷题 Day39

62. Unique Paths

There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time.

Given the two integers m and n, return the number of possible unique paths that the robot can take to reach the bottom-right corner.

The test cases are generated so that the answer will be less than or equal to 2 * 109.

 Example 1:

Input: m = 3, n = 7
Output: 28

Example 2:

Input: m = 3, n = 2
Output: 3
Explanation: From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
1. Right -> Down -> Down
2. Down -> Down -> Right
3. Down -> Right -> Down

思路: 1. 定义dp数组的下标和值: 下标为当前位置,数值为到达当前位置选择个数 2. 确定递推公式: 这个和爬楼梯有点类似,但是根据题意,应该使用一个二维dp数组做推导。 dp[i] = d[i - 1][j] + d[i][j - 1]; 3. dp数组初始化, m = 0或 n = 0时 ,dp数值为1,其余为0 4. 遍历顺序: 从左上到右下 5. 打印dp

代码:

var uniquePaths = function(m, n) {
    let res = [];
    for (let i = 0; i < m; i++) {
        res.push(Array(n).fill(0));
    }


    for (let i = 0; i < m; i++) {
        res[i][0] = 1;
    }

    for (let i= 0; i < n; i++) {
        res[0][i] = 1;
    }

    for (let i = 1; i < m; i++) {
        for (let j = 1; j < n; j++) {
            res[i][j] = res[i - 1][j] + res[i][j - 1];
        }
    }

    return res[m - 1][n - 1];
};

时间复杂度: O(m * n) 空间复杂度: O(m * n)


63. Unique Paths II

You are given an m x n integer array grid. There is a robot initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time.

An obstacle and space are marked as 1 or 0 respectively in grid. A path that the robot takes cannot include any square that is an obstacle.

Return the number of possible unique paths that the robot can take to reach the bottom-right corner.

The testcases are generated so that the answer will be less than or equal to 2 * 109.

Example 1:

Input: obstacleGrid = [[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

Example 2:

Input: obstacleGrid = [[0,1],[0,0]]
Output: 1

思路:

  1. dp下标和数据含义: 下标为当前位置,数据意义为走到当前位置的路径个数
  2. 确定递推公式: 和62题一样,dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
  3. dp初始化: 还是第一行和第一列设置为1, 但是如果第一行或第一列有障碍物的话,障碍物之后的值仍然是0
  4. 遍历顺序: 从左上到右下
  5. 打印dp

代码:

var uniquePathsWithObstacles = function(obstacleGrid) {
    let m = obstacleGrid.length; 
    let n =  obstacleGrid[0].length;
    for (let i = 0; i < m; i++) {
        for (let j = 0; j < n; j++) {
            obstacleGrid[i][j] = obstacleGrid[i][j] === 1 ? -1 : 0;
        }
    }

    for (let i = 0; i < m; i++) {
        if (obstacleGrid[i][0] !== -1) {
                obstacleGrid[i][0] = 1;
        } else {
            break;
        }
    }


    for (let i = 0; i < n; i++) {
        if (obstacleGrid[0][i] !== -1) {
                obstacleGrid[0][i] = 1;
        } else {
            break;
        }
    }

    for (let i = 1; i < m; i++) {
        for (let j = 1; j < n; j++) {
            if (obstacleGrid[i][j] === -1) continue;

            const up = obstacleGrid[i - 1][j] === -1 ? 0 : obstacleGrid[i - 1][j];
            const left = obstacleGrid[i][j - 1] === -1 ? 0 : obstacleGrid[i][j - 1];

            obstacleGrid[i][j] = up + left;
        }
    }

    return obstacleGrid[m - 1][n - 1] === -1 ? 0 : obstacleGrid[m - 1][n - 1];
};

时间复杂度: O(m * n) 空间复杂度: O(1),只在obstacleGrid做出数据改变