-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnique Paths.py
25 lines (19 loc) · 875 Bytes
/
Unique Paths.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# Link: https://leetcode.com/problems/unique-paths/submissions/
class Solution(object):
def uniquePaths(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
"""
# Create a matrix
matrix = [[1] * n] * m
# Traverse matrix from neighboring top-left cell
for xPos in range(1, m):
for yPos in range(1, n):
# Each cell will have a number representing how many ways there are to reach them.
# Add the left cell and top cell to get the number of ways to reach current cell.
# Do this to every single cell inside matrix.
matrix[xPos][yPos] = matrix[xPos][yPos - 1] + matrix[xPos - 1][yPos]
# Return the value of the bottom-right cell
return matrix[-1][-1]