-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdp_uniquePaths.py
More file actions
236 lines (180 loc) · 6.44 KB
/
dp_uniquePaths.py
File metadata and controls
236 lines (180 loc) · 6.44 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#Problem:
# Unique Paths
#
# A robot is located at the top-left corner of an m x n grid (marked 'S' in the diagram below)
# The robot can only move either down or right at any point in time.
# The robot is tryingto reach the bottom-right corner of the grid (marked 'F' in the diagram below).
#
# How many possible unique paths are there?
#
# +---+---+---+---+
# | S | | | |
# +---+---+---+---+
# | | | | |
# +---+---+---+---+
# | | | | E |
# +---+---+---+---+
def uniquePaths(input: [[int]]) -> int:
m = len(input)
n = len(input[0])
input[0][0] = 1
for i in range(0,m):
for j in range(0,n):
if i == 0 and j == 0:
continue
elif i > 0 and j > 0:
input[i][j] = input[i][j-1] + input[i-1][j]
elif j == 0:
input[i][j] = input[i-1][j]
else: #i = 0
input[i][j] = input[i][j-1]
return input[m-1][n-1]
#Follow-up question:
# If there are blockages in our path, how will we solve the problem?
#
# How many possible unique paths are there?
#
# +---+---+---+---+
# | S | | | |
# +---+---+---+---+
# | | x | x | x |
# +---+---+---+---+
# | | | | E |
# +---+---+---+---+
#---------------------------------------------------------
# !!!!!!!!!! correct but long way. too many comparisons! !!!!!!!!!!
#---------------------------------------------------------
# F(i,j) = F(i,j-1) + F(i-1, j)
# def uniquePathsWithObstacles(matrix: [[int]]) -> int:
# m = len(matrix)
# n = len(matrix[0])
# matrix[0][0] = 1
# for i in range(0,m):
# for j in range(0,n):
# if i == 0 and j == 0:
# continue
# elif i > 0 and j > 0:
# if matrix[i][j] == 'x':
# continue
# else:
# if matrix[i][j-1] == 'x' and matrix[i-1][j] == 'x':
# continue
# elif matrix[i][j-1] == 'x':
# matrix[i][j] = matrix[i-1][j]
# elif matrix[i-1][j] == 'x':
# matrix[i][j] = matrix[i][j-1]
# else:
# matrix[i][j] = matrix[i][j-1] + matrix[i-1][j]
# elif j == 0:
# if matrix[i][j] == 'x':
# continue
# elif matrix[i-1][j] == 'x':
# continue
# else:
# matrix[i][j] = matrix[i-1][j]
# else: #i = 0
# if matrix[i][j] == 'x':
# continue
# elif matrix[i][j-1] == 'x':
# continue
# else:
# matrix[i][j] = matrix[i][j-1]
# return matrix[m-1][n-1]
#above code is very tedious as you have to perform many checks.
#------------------------------------------------------------------------
# below is demonstrated a simpler way to implement the obstacle question
#-------------------------------------------------------------------------
def uniquePathsWithObstacles(input: [[int]]) -> int:
m = len(input) #row
n = len(input[0]) #column
input[0][0] = 1
for i in range(0,m):
for j in range(0,n):
if i == 0 and j == 0:
continue
elif input[i][j] == 'x':
input[i][j] = 0
continue
elif i > 0 and j > 0:
input[i][j] = input[i][j-1] + input[i-1][j]
elif j == 0:
input[i][j] = input[i-1][j]
else:
input[i][j] = input[i][j-1]
return input[m-1][n-1]
#Optimization problem:
# Maximum Profit in a grid
#
# A robot is located at the top-left corner of a m x n grid
# The robot can only move either down or right at any point in time.
# The robot is trying to reach the bottom-right corner of the grid
# Each cell contains a coin the robot can collect.
#
# What is the maximum profit the robot can gather?
#
# +---+---+---+---+
# | S | 2 | 2 | 1 |
# +---+---+---+---+
# | 3 | 1 | 1 | 1 |
# +---+---+---+---+
# | 4 | 4 | 2 | E |
# +---+---+---+---+
def uniquePathsCosts(input: [[int]]) -> int:
m = len(input) #row
n = len(input[0]) #column
dpMatrix = [[0 for i in range(n)] for j in range(m)]
for i in range(m):
for j in range(n):
dpMatrix[i][j] = input[i][j]
if i > 0 and j > 0:
dpMatrix[i][j] += max(dpMatrix[i][j-1], dpMatrix[i-1][j])
elif j > 0:
dpMatrix[i][j] += dpMatrix[i][j-1]
else:
dpMatrix[i][j] += dpMatrix[i-1][j]
return dpMatrix[i][j]
def uniquePathsCostsWithRoute(input: [[int]]) -> ():
m = len(input) #rows
n = len(input[0]) #columns
dpMatrix = [[(0, (0, 0)) for j in range(n)] for i in range(m)]
for i in range(m):
for j in range(n):
if i == 0 and j == 0:
continue
tempTuple = (input[i][j], (i,j))
dpMatrix[i][j] = tempTuple
if i > 0 and j > 0:
profit = input[i][j] + max(dpMatrix[i][j-1][0], dpMatrix[i-1][j][0])
if input[i][j-1] >= input[i-1][j]:
prevIndex = (i,j-1)
else:
prevIndex = (i-1, j)
tempTuple = (profit, prevIndex)
dpMatrix[i][j] = tempTuple
elif j > 0:
profit = input[i][j] + dpMatrix[i][j-1][0]
prevIndex = (i,j-1)
tempTuple = (profit, prevIndex)
dpMatrix[i][j] = tempTuple
else: # i > 0
profit = input[i][j] + dpMatrix[i-1][j][0]
prevIndex = (i-1,j)
tempTuple = (profit, prevIndex)
dpMatrix[i][j] = tempTuple
#return route
i = m - 1
j = n - 1
routeArray = []
routeArray.append((i,j))
while i > 0 or j > 0:
tempTuple = dpMatrix[i][j]
routeArray.append(tempTuple[1])
i = tempTuple[1][0]
j = tempTuple[1][1]
routeArray.reverse()
return (dpMatrix[m-1][n-1][0], routeArray)
matrix = [[0,2,2,50],
[3,1,1,100],
[4,4,2,0]]
a = uniquePathsCostsWithRoute(matrix)
print(" total profit is {0} \n and route to take is {1}".format(a[0], a[1]))