Skip to main content

62 - Unique Paths

Details

KeyValue
Linkhttps://leetcode.com/problems/unique-paths/
LanguagePython 3
Runtime40 ms, faster than 76.07% of Python3 online submissions for Unique Paths
Memory Usage13.9 MB, less than 73.95% of Python3 online submissions for Unique Paths
DatastructuresList[int]
AlgorithmsDP
ComplexityTime: O(N*M) Memory: O(N)

Procedure

  1. ...

Code

class Solution:
def uniquePaths(self, m: int, n: int) -> int:
# Start by creating the bottom row. The
# bottom row should be all 1s as there is
# only one way to get to the bottom right.
row = [1] * n

for _ in range(m-1):
new_row = [1] * n
for col in range(n-2, -1, -1):
new_row[col] = new_row[col+1] + row[col]
row = new_row

return row[0]