Unique Paths

To solve this coding challenge, we need to determine the number of unique paths a robot can take to move from the top-left corner to the bottom-right corner of a grid. The robot can only move down or right at any point in time, and we are given the dimensions of the grid (m, n). Let's explore the approach and create pseudocode to solve this problem.
# Explanation
The challenge involves computing the number of unique paths from the top-left to the bottom-right of an m x n grid. The robot can only move down or right. A dynamic programming approach is suitable for this problem because it allows us to break the problem down into smaller subproblems and reuse their solutions.
## Key Points:
  1. Grid Representation : Use a 2D array (e.g., dp) where dp[i][j] represents the number of unique paths to reach cell (i, j).
  2. Initialization :
    • The first row (dp[0][j]) should all be 1 because the only way to move right in the first row is from the left.
    • The first column (dp[i][0]) should all be 1 because the only way to move down in the first column is from the top.
  3. State Transition : For each cell (i, j), the number of unique paths to that cell is the sum of the number of unique paths to the cell directly above it (dp[i-1][j]) and the cell directly to its left (dp[i][j-1]).
  4. Detailed Steps in Pseudocode:
  5. Initialize a matrix (dp) with dimensions m x n. Start by filling the entire matrix with default values (e.g., 1).
  6. Iterate over the cells of the matrix starting from (1, 1) . Update each cell by summing the values from the top and left cells.
  7. Return the value in the bottom-right corner of the matrix (dp[m-1][n-1]) which holds the total number of unique paths.
  8. # Pseudocode with Comments:
                                                
    function uniquePaths(m, n):
    # Create a 2D list (dp) with m rows and n columns initialized to 1
    dp = initialize 2D list of size m x n filled with 1
    
    # Loop over each row starting from the second row
    for row in range(1, m):
    # Loop over each column starting from the second column
    for col in range(1, n):
    # Update the cell dp[row][col]
    # It is the sum of the value from the cell above (dp[row-1][col])
    # and the value from the cell on the left (dp[row][col-1])
    dp[row][col] = dp[row - 1][col] + dp[row][col - 1]
    
    # Return the value in the bottom-right cell which contains the number of unique paths
    return dp[m - 1][n - 1]
    
                                            
    Step-by-Step Explanation
  9. Initialize the dp matrix : We first create a 2D list with dimensions m x n, where every element is initialized to 1. This is because the first row and first column should all be 1, representing the only ways to move to these cells (either only right or only down).
  10. Iterating through the matrix : We start with the second row and the second column. For each cell at position (i, j), we calculate the number of ways to reach it by summing the number of ways to reach the cell above it and the cell to the left.
  11. Compute the number of paths dynamically : Each cell in the matrix dp is filled based on previously computed values, ensuring that by the time we reach the bottom-right corner, dp[m-1][n-1] will have the total number of unique paths from the top-left to bottom-right.
By following this approach, the robot's movement paths are computed efficiently using dynamic programming, ensuring that the solution scales well with the given constraints (1 <= m, n <= 100).