Skip to main content

Unique Paths

Definition​

The Unique Paths Algorithm calculates the number of unique paths from the top-left corner to the bottom-right corner of a grid, moving only down or right

Practice​

uniquePaths(m, n):
# Initialize a 2D array to store the number of unique paths
grid = [[0] * n for _ in range(m)]

# Set the starting point to 1
grid[0][0] = 1

# Iterate through each cell
for i from 0 to m-1:
for j from 0 to n-1:
# Update the number of unique paths for the current cell
if i > 0:
grid[i][j] += grid[i-1][j]
if j > 0:
grid[i][j] += grid[i][j-1]

# Return the number of unique paths to the bottom-right corner
return grid[m-1][n-1]