Unique Paths
Clarify the problem:
- The problem requires us to find the number of unique paths from the top-left corner to the bottom-right corner of a grid.
- We can only move either down or right at any point in time.
- The grid is represented as a 2D matrix, and we are given its dimensions.
Analyze the problem:
- Input: The dimensions of the grid (m x n).
- Output: The number of unique paths from the top-left to the bottom-right corner of the grid.
- Constraints:
- The dimensions m and n are positive integers.
- The grid can be of varying sizes.
Design an algorithm:
- We can solve this problem using dynamic programming.
- Create a 2D grid of size m x n to store the number of unique paths for each position.
- Initialize the first row and first column of the grid to 1 since there is only one way to reach any position in the first row or column.
- For each cell (i, j) in the grid, the number of unique paths is the sum of the number of paths from the cell above (i-1, j) and the cell to the left (i, j-1).
- Traverse the grid from left to right and top to bottom, updating each cell with the number of unique paths.
- Return the value in the bottom-right corner of the grid, which represents the total number of unique paths.
Explain your approach:
- We will define a function
uniquePaths
that takes the dimensions m and n as input. - Create a 2D grid of size m x n and initialize all cells to 0.
- Initialize the first row and first column of the grid to 1.
- Traverse the grid from the second row and second column.
- For each cell (i, j), update its value as the sum of the value in the cell above (i-1, j) and the value in the cell to the left (i, j-1).
- Finally, return the value in the bottom-right corner of the grid.
- We will define a function
Write clean and readable code:
python
def uniquePaths(m, n):
# Create a 2D grid of size m x n
grid = [[0] * n for _ in range(m)]
# Initialize the first row and first column to 1
for i in range(m):
grid[i][0] = 1
for j in range(n):
grid[0][j] = 1
# Traverse the grid and update each cell with the number of unique paths
for i in range(1, m):
for j in range(1, n):
grid[i][j] = grid[i-1][j] + grid[i][j-1]
# Return the value in the bottom-right corner
return grid[m-1][n-1]
- Test your code:
python
# Test case 1:
m1, n1 = 3, 7
result1 = uniquePaths(m1, n1)
# Expected output: 28 (3x7 grid has 28 unique paths)
# Test case 2:
m2, n2 = 3, 2
result2 = uniquePaths(m2, n2)
# Expected output: 3 (3x2 grid has 3 unique paths)
# Test case 3:
m3, n3 = 7, 3
result3 = uniquePaths(m3, n3)
# Expected output: 28 (7x3 grid has 28 unique paths)
# Test case 4:
m4, n4 = 1, 1
result4 = uniquePaths(m4, n4)
# Expected output: 1 (1x1 grid has only 1 unique path)
# Test case 5:
m5, n5 = 2, 2
result5 = uniquePaths(m5, n5)
# Expected output: 2 (2x2 grid has 2 unique paths)
# Test case 6:
m6, n6 = 0, 0
result6 = uniquePaths(m6, n6)
# Expected output: 0 (Empty grid has 0 unique paths)
# Print the results
print(result1)
print(result2)
print(result3)
print(result4)
print(result5)
print(result6)
Optimize if necessary:
- The solution provided already has an optimal time complexity of O(m*n) since we traverse the entire grid once.
Handle error cases:
- The solution handles the case when either m or n is 0 by returning 0, indicating that there are no unique paths in an empty grid.
- We assume that the input dimensions m and n are non-negative integers.
Discuss complexity analysis:
- Time complexity: The time complexity of the solution is O(m*n) since we traverse the entire grid once.
- Space complexity: The space complexity is O(m*n) as we create a 2D grid to store the number of unique paths for each position.