Unique Paths in Python

def uniquePaths(m, n):
    # Step 1: Initialize a 2D array with zeros
    dp = [[0]*n for _ in range(m)]
    
    # Step 2: Set the base case
    for i in range(m):
        dp[i][0] = 1
    for j in range(n):
        dp[0][j] = 1
    
    # Step 3: Fill the matrix
    for i in range(1, m):
        for j in range(1, n):
            dp[i][j] = dp[i-1][j] + dp[i][j-1]
    
    # Step 4: Return the value at the bottom-right corner
    return dp[m-1][n-1]

PrevNext