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 2109.
 

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:
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];
}

相关推荐

  1. 面试经典150题(62-64)

    2024-02-14 13:54:02       29 阅读
  2. LeetCode[62] 不同路径

    2024-02-14 13:54:02       32 阅读
  3. leetcode 62.不同路径

    2024-02-14 13:54:02       14 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-14 13:54:02       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-14 13:54:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-14 13:54:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-14 13:54:02       20 阅读

热门阅读

  1. Qt杂记——TCP

    2024-02-14 13:54:02       29 阅读
  2. 人工智能之数学基础【最小二乘法】

    2024-02-14 13:54:02       27 阅读
  3. Lua weak表

    2024-02-14 13:54:02       33 阅读
  4. nginx命名location跳转的模块上下文继承

    2024-02-14 13:54:02       31 阅读
  5. Oracle NLSSORT 拼音排序 笔画排序 部首排序

    2024-02-14 13:54:02       28 阅读
  6. 【算法题】102. 二叉树的层序遍历

    2024-02-14 13:54:02       31 阅读
  7. openJudge | 单词倒排 C语言

    2024-02-14 13:54:02       35 阅读
  8. 计算机视觉基础:矩阵运算

    2024-02-14 13:54:02       28 阅读
  9. UDP报文结构和注意事项

    2024-02-14 13:54:02       32 阅读
  10. C入门番外篇——师兄的不耻下问(2024是个闰年)

    2024-02-14 13:54:02       28 阅读