马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
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.
- Input: m = 3, n = 7
- Output: 28
- 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
复制代码 动态规划
思绪
- 界说一个二维数组 dp,此中 dp[j] 体现从出发点 (0, 0) 到点 (i, j) 的路径数。
- 呆板人只能向右或向下移动,因此状态转移方程为: dp[j] = dp[i - 1][j] + dp[j - 1]
- 初始化:第一行和第一列的全部格子只有一种路径(只能不停向右或不停向下),因此 dp[0][j] = 1 和 dp[0] = 1。
实当代码
- int uniquePaths(int m, int n) {
- // 定义 dp 数组
- vector<vector<int>> dp(m, vector<int>(n, 1));
- // 填充 dp 数组
- for (int i = 1; i < m; i++) {
- for (int j = 1; j < n; j++) {
- dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
- }
- }
- return dp[m - 1][n - 1];
- }
复制代码 复杂度分析
- 时间复杂度:O(m * n),必要添补整个 dp 数组。
- 空间复杂度:O(m * n),必要一个二维数组存储中心结果。
组合数学
思绪
- 从出发点 (0, 0) 到尽头 (m - 1, n - 1),呆板人必要移动 m - 1 次向下和 n - 1 次向右。
- 总移动次数为 (m - 1) + (n - 1) = m + n - 2。
- 题目转化为从 m + n - 2 次移动中选择 m - 1 次向下(或 n - 1 次向右)的组合数。
- 组合数公式为:C(m + n - 2, m - 1) = (m + n - 2)! / ((m - 1)! * (n - 1)!)
实当代码
- int uniquePaths(int m, int n) {
- // 计算组合数 C(m + n - 2, m - 1)
- long long result = 1;
- int total = m + n - 2; // 总移动次数
- int k = min(m - 1, n - 1); // 选择较小的值计算组合数
- for (int i = 1; i <= k; i++) {
- result = result * (total - k + i) / i;
- }
- return (int)result;
- }
复制代码 复杂度分析
- 时间复杂度:O(min(m, n)),只必要盘算组合数。
- 空间复杂度:O(1),只利用了常数空间。
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.
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
复制代码 这个题目是带停滞物的呆板人路径题目,可以通过动态规划来办理。我们必要思量停滞物对路径的影响,并在动态规划的过程中跳过停滞物。
动态规划
界说状态
设 dp[j] 体现从出发点 (0, 0) 到点 (i, j) 的路径数。
状态转移方程
- 假如 grid[j] == 1(停滞物),则 dp[j] = 0,由于无法通过停滞物。
- 否则,dp[j] = dp[i - 1][j] + dp[j - 1],即从上方或左方到达当前点的路径数之和。
初始化
- 假如出发点 (0, 0) 是停滞物,则直接返回 0,由于无法开始移动。
- 初始化第一行和第一列:
- 假如某个格子是停滞物,则它及厥后续格子的路径数都为 0。
- 否则,路径数为 1(只能不停向右或不停向下)。
- int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
- int m = obstacleGrid.size(), n = obstacleGrid[0].size();
- vector<vector<int>> dp(m, vector<int>(n, 0));
- dp[0][0] = 1;
- for (int i = 1; i < m; i++) {
- if (obstacleGrid[i][0] == 1) {
- dp[i][0] = 1;
- }
- else {
- dp[i][0] = dp[i - 1][0];
- }
- }
- for (int j = 1; j < n; j++) {
- if (obstacleGrid[0][j] == 1) {
- dp[0][j] = 0;
- }
- else {
- dp[0][j] = dp[0][j - 1];
- }
- }
- for (int i = 1; i < m; i++) {
- for (int j = 1; j < n; j++) {
- if (obstacleGrid[i][j] == 1) {
- dp[i][j] = 0;
- }
- else {
- dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
- }
- }
- }
- return dp[m - 1][n - 1];
- }
复制代码 复杂度分析
- 时间复杂度:O(m * n),必要遍历整个网格。
- 空间复杂度:O(m * n),必要一个二维数组存储中心结果。
优化空间复杂度
我们可以将空间复杂度优化为 O(n),由于每次更新 dp[j] 时只必要用到当前行和前一行的数据。
状态转移方程
对于每一行 i,从左到右更新 dp[j]:dp[j] = dp[j] + dp[j - 1]
- dp[j] 体现从上方来的路径数(即上一行的 dp[j])。
- dp[j - 1] 体现从左方来的路径数(即当前行的 dp[j - 1])。
- int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
- int m = obstacleGrid.size();
- int n = obstacleGrid[0].size();
- if (obstacleGrid[0][0] == 1 || obstacleGrid[m - 1][n - 1] == 1) {
- return 0;
- }
- vector<int> dp(n, 0);
- dp[0] = 1;
- for (int i = 0; i < m; i++) {
- for (int j = 0; j < n; j++) {
- if (obstacleGrid[i][j] == 1) {
- dp[j] = 0;
- } else if (j > 0) {
- dp[j] += dp[j - 1];
- }
- }
- }
- return dp[n - 1];
- }
复制代码 |