Skip to content

Commit e4ac1df

Browse files
Combinatorics
1 parent c8db17a commit e4ac1df

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Math/Grid Unique Path.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# https://www.interviewbitcom/problems/grid-unique-paths/
2+
3+
# Grid Unique Path
4+
5+
# Input : A = 2, B = 2
6+
# Output : 2
7+
# 2 possible routes : (0, 0) -> (0, 1) -> (1, 1)
8+
# OR : (0, 0) -> (1, 0) -> (1, 1)
9+
10+
11+
class Solution:
12+
# @param A : integer
13+
# @param B : integer
14+
# @return an integer
15+
def uniquePaths(self,rows, cols):
16+
17+
if rows==1 or cols == 1:
18+
return 1
19+
20+
a = [[1] + [0]*(cols-1)]*rows
21+
a[0] = [1]*cols
22+
23+
for i in range(1,rows):
24+
for j in range(1,cols):
25+
a[i][j] = a[i-1][j] + a[i][j-1]
26+
27+
28+
29+
return a[rows-1][cols-1]

0 commit comments

Comments
 (0)