forked from netsetos/python_code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnique Path
More file actions
35 lines (27 loc) · 796 Bytes
/
Unique Path
File metadata and controls
35 lines (27 loc) · 796 Bytes
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
26
27
28
29
30
31
32
33
34
35
#Recursive Approach
def path(arr,X,Y):
sizeX = len(X)
sizeY = len(Y)
if(X >= sizeX or Y >= sizeY):
return 0
elif (arr[X][Y] == 1):
return 0
elif(X == sizeX-1 and Y == sizeY-1):
return 1
return self.path(arr, X + 1, Y) + self.path(arr, X, Y + 1)
#Dynamic Programming
def UniquePath(arr):
dp=[[0] *len(arr[0])for i in arr]
if(arr[0][0]==0):
dp[0][0]=1
for i in range(1,len(arr)):
if arr[i][0]==0:
dp[i][0]=dp[i-1][0]
for j in range(1,len(arr[0])):
if arr[0][j]==0:
dp[0][j]=dp[0][j-1]
for i in range(1,len(arr)):
for j in range(1,len(arr[0])):
if arr[i][j]==0:
dp[i][j]=dp[i-1][j]+dp[i][j-1]
return dp[-1][-1]