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., 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 ∗ 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 dp[j] 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 (dp[j - 1]) to the current value of dp[j], 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 (dp[n - 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];
}
相关推荐
ShiinaMashirol42 分钟前
代码随想录打卡|Day27(合并区间、单调递增的数字、监控二叉树)
java·算法
梁下轻语的秋缘2 小时前
每日c/c++题 备战蓝桥杯(P1049 [NOIP 2001 普及组] 装箱问题)
c语言·c++·学习·蓝桥杯
加点油。。。。3 小时前
C语言高频面试题——指针函数和函数指针的区别
c语言·面试
wuqingshun3141593 小时前
蓝桥杯 5. 交换瓶子
数据结构·c++·算法·职场和发展·蓝桥杯
Demons_kirit3 小时前
Leetcode 2845 题解
算法·leetcode·职场和发展
adam_life3 小时前
http://noi.openjudge.cn/——2.5基本算法之搜索——200:Solitaire
算法·宽搜·布局唯一码
我想进大厂4 小时前
图论---朴素Prim(稠密图)
数据结构·c++·算法·图论
我想进大厂4 小时前
图论---Bellman-Ford算法
数据结构·c++·算法·图论
AIGC大时代4 小时前
高效使用DeepSeek对“情境+ 对象 +问题“型课题进行开题!
数据库·人工智能·算法·aigc·智能写作·deepseek
lkbhua莱克瓦244 小时前
用C语言实现——一个中缀表达式的计算器。支持用户输入和动画演示过程。
c语言·开发语言·数据结构·链表·学习方法·交友·计算器