Unique Paths
LeetCode 62 · View on LeetCode
Count paths in a grid from top-left to bottom-right, moving only right or down. Each cell's path count equals the sum of the cell above and to the left. Optimize to a single row.
def unique_paths(m: int, n: int) -> int:
row = [1] * n
for _ in range(1, m):
for col in range(1, n):
row[col] += row[col - 1]
return row[-1]
if __name__ == '__main__':
print(unique_paths(3, 7)) # 28
print(unique_paths(3, 2)) # 3
print(unique_paths(1, 1)) # 1
print(unique_paths(7, 3)) # 28