LeetCode //C - 62. Unique Paths

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., grid00). The robot tries to move to the bottom-right corner (i.e., gridm - 1n - 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 ∗ 1 0 9 2 * 10^9 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
Constraints:
  • 1 <= m, n <= 100

From: LeetCode

Link: 62. Unique Paths


Solution:

Ideas:
  1. Initialization: A 1-dimensional array dp\[\] with n elements is created. Since the robot can only move right or down, there is exactly one way to reach any cell in the first row of the grid (by moving right at every step). Therefore, the dp\[\] array is initialized with 1s.

  2. Dynamic Programming: The code then iterates through the grid row by row, starting from the second row (since the first row is already initialized). For each cell (i, j) (excluding those in the first row and column), the number of unique paths to that cell is the sum of the unique paths to the cell directly above it (i - 1, j) and the cell to the left of it (i, j - 1). This is because the robot can only arrive at (i, j) from these two adjacent cells.

  3. Filling the dp\[\] Array: For each row, the code updates the dp\[\] array in place. Each dpj will hold the count of unique paths to reach the cell (i, j). The update is done by adding the number of paths to the cell to the left (dpj - 1) to the current value of dpj, which before the update holds the number of paths to the cell above.

  4. Result: After filling in the dp\[\] array for all rows, the last element of the dp\[\] array (dpn - 1) will contain the number of unique paths to reach the bottom-right corner of the grid.

Caode:
c 复制代码
int uniquePaths(int m, int n) {
    // We only need one row to store the previous results
    int dp[n];
    
    // Initialize the first row to 1's since there's only one way to reach any cell in the first row
    for (int i = 0; i < n; i++) {
        dp[i] = 1;
    }
    
    // Build the number of ways to get to each cell
    // We start at 1 since the first row is already initialized
    for (int i = 1; i < m; i++) {
        // We start at 1 here as well since there's only one way to reach any cell in the first column
        for (int j = 1; j < n; j++) {
            // The number of ways to get to the current cell is the sum of the ways to get to the cell above and the cell to the left
            dp[j] += dp[j - 1];
        }
    }
    
    // The bottom-right corner will have our answer
    return dp[n - 1];
}
相关推荐
ziguo11221 小时前
深入浅出 C/C++ 数据类型:从入门到踩坑
linux·c语言·c++·windows·visual studio
元Y亨H1 小时前
开发者必须掌握的十大核心算法
算法
元Y亨H1 小时前
深度解构:数据结构与算法的理论基石与工程演进
数据结构·算法
元Y亨H2 小时前
数据结构与算法的通俗指南
数据结构·算法
2301_764441332 小时前
用动力学系统(微分方程)为 Kernberg 的客体关系单元提供数学化的操作定义,把“自体—客体“这对心理结构建模成一个二维耦合系统
数据结构·python·算法·数学建模
林泽毅2 小时前
PyTRIO快速入门(二):Datum构建
人工智能·算法·产品
keep intensify2 小时前
最长有效括号
算法·leetcode·动态规划
CoderYanger2 小时前
A.每日一题:1979. 找出数组的最大公约数
java·程序人生·算法·leetcode·面试·职场和发展·学习方法
猫头虎3 小时前
什么是ZCode for GLM-5.2?
开发语言·人工智能·python·科技·算法·ai编程·ai写作
长不胖的路人甲3 小时前
什么是赫夫曼树(哈夫曼树 / Huffman Tree)
python·算法·霍夫曼树