当前位置: 首页 > article >正文

⭐算法OJ⭐矩阵的相关操作【动态规划 + 组合数学】(C++ 实现)Unique Paths 系列

文章目录

  • 62. Unique Paths
    • 动态规划
      • 思路
      • 实现代码
      • 复杂度分析
    • 组合数学
      • 思路
      • 实现代码
      • 复杂度分析
  • 63. Unique Paths II
    • 动态规划
      • 定义状态
      • 状态转移方程
      • 初始化
      • 复杂度分析
    • 优化空间复杂度
      • 状态转移方程

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[i][j] 表示从起点 (0, 0) 到点 (i, j) 的路径数。

  • 机器人只能向右或向下移动,因此状态转移方程为: dp[i][j] = dp[i - 1][j] + dp[i][j - 1]

  • 初始化:第一行和第一列的所有格子只有一种路径(只能一直向右或一直向下),因此 dp[0][j] = 1dp[i][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[i][j] 表示从起点 (0, 0) 到点 (i, j) 的路径数。

状态转移方程

  • 如果 grid[i][j] == 1(障碍物),则 dp[i][j] = 0,因为无法通过障碍物。
  • 否则,dp[i][j] = dp[i - 1][j] + dp[i][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[i][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];
}

http://www.kler.cn/a/571075.html

相关文章:

  • ADC采集模块与MCU内置ADC性能对比
  • 【uniapp】子组件和父组件双向绑定,vue3已废除sync写法,v-model代替
  • 计算机毕业设计SpringBoot+Vue.js手机商城 (源码+文档+PPT+讲解)
  • blender学习25.3.3
  • 网络原理--HTTP协议
  • Ubuntu系统上部署Node.js项目的完整流程
  • kafka-关于ISR-概述
  • mapbox基础,使用点类型geojson加载symbol符号图层,用于标注文字
  • C#用加密安全的伪随机数生成器 (CSPRNG) 生成以太坊地址
  • CDefView::_OnFSNotify函数分析
  • 如何在.NET Core中解决缓存穿透、缓存雪崩和缓存击穿问题:多级缓存策略详解
  • 人工智能之数学基础:线性代数中矩阵的初印象
  • 使用MATLAB结合EasySpin进行ESR模拟的详细步骤及示例代码
  • Sqlserver安全篇之_启用TLS即配置SQL Server 数据库引擎以加密连接
  • 【智能机器人开发全流程:硬件选型、软件架构与ROS实战,打造高效机器人系统】
  • 影刀RPA开发拓展--SQL常用语句全攻略
  • AWS Amazon Aurora MySQL 性能监控与安全治理实战指南
  • Metal学习笔记十一:贴图和材质
  • 变分自编码器(Variational Autoencoder, VAE)中的解码器(Decoder)详解
  • 简述Spark的宽窄依赖以及Stage是怎么划分的以及每个stage又是怎么划分task任务数