Skip to content

Commit 77835c7

Browse files
author
Hamid Gasmi
committed
#167 is completed with a DP technique + buttom up approach
1 parent febbbc3 commit 77835c7

File tree

8 files changed

+111
-0
lines changed

8 files changed

+111
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import sys
2+
3+
# 1. Express a solution mathematically: Let's be a Matrix M of (n+1) x (m+1):
4+
# For 0 <= r <= n and 0 <= c <= m, M[r,c] contains the longest path from the source (0, 0) to (r, c)
5+
# M[0 , 0] = 0
6+
# M[0 , c] = M[0 , c - 1] + right[0, c] for 1 <= c <= m
7+
# M[r , 0] = M[r - 1 , 0] + down[r , 0] for 1 <= r <= n
8+
# M[r , c] = max(M[r - 1 , c] + down[r - 1 , c], M[r , c - 1] + right[r , c - 1])
9+
# 2. Proof:
10+
# Let's assume that there is a vertice (r, c) that belongs to the optimal path P with a the longest path length |P|
11+
# But M[r , c] < max(M[r - 1 , c] + down[r - 1 , c], M[r , c - 1] + right[r , c - 1])
12+
# This means that if we replace M[r , c] with max(M[r - 1 , c] + down[r - 1 , c], M[r , c - 1] + right[r , c - 1])
13+
# The new path P length |P'| will be greater than |P| ==> contradiction with the fact that |P| was the longest path
14+
# 3. Implementation:
15+
# Buttom up solution
16+
# Running Time: O(nm) (Quadratic)
17+
# Space complexity: O(nm) (Quadratic)
18+
class Solution:
19+
def __init__(self, n, m):
20+
self.rows_count = n + 1
21+
self.columns_count = m + 1
22+
23+
def longest_path(self, down, right):
24+
25+
M = [ [0 for _ in range(self.columns_count)] for _ in range(self.rows_count) ]
26+
27+
for c in range(1, self.columns_count, 1):
28+
M[0][c] = M[0][c - 1] + right[0][c - 1]
29+
30+
for r in range(1, self.rows_count, 1):
31+
M[r][0] = M[r - 1][0] + down[r - 1][0]
32+
33+
for r in range(1, self.rows_count, 1):
34+
for c in range(1, self.columns_count, 1):
35+
candidate_predecesor_top = M[r - 1][c] + down[r - 1][c]
36+
candidate_predecesor_left = M[r][c - 1] + right[r][c - 1]
37+
M[r][c] = max(candidate_predecesor_top, candidate_predecesor_left)
38+
39+
return M[self.rows_count - 1][self.columns_count - 1]
40+
41+
if __name__ == "__main__":
42+
n,m = map(int, sys.stdin.readline().strip().split())
43+
down = [list(map(int, sys.stdin.readline().strip().split()))
44+
for _ in range(n)]
45+
sys.stdin.readline()
46+
right = [list(map(int, sys.stdin.readline().strip().split()))
47+
for _ in range(n+1)]
48+
49+
s = Solution(n, m)
50+
print(s.longest_path(down, right))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2 1
2+
1 2
3+
2 1
4+
-
5+
4
6+
3
7+
5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2 2
2+
20 0 0
3+
20 0 0
4+
-
5+
0 0
6+
0 0
7+
10 10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
4 4
2+
1 0 2 4 3
3+
4 6 5 2 1
4+
4 4 5 2 1
5+
5 6 8 5 3
6+
-
7+
3 2 4 0
8+
3 2 4 2
9+
0 7 3 3
10+
3 3 0 2
11+
1 3 2 2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2 2
2+
0 0 20
3+
0 0 20
4+
-
5+
10 10
6+
0 0
7+
0 0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2 2
2+
20 0 0
3+
0 0 0
4+
-
5+
0 30
6+
0 0
7+
0 0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
5 3
2+
20 5 0 10
3+
0 5 10 0
4+
10 10 0 15
5+
0 20 20 25
6+
30 10 5 30
7+
-
8+
0 30 15
9+
10 20 10
10+
10 10 20
11+
20 25 30
12+
15 35 40
13+
15 10 25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
3 5
2+
0 5 10 0 10 10
3+
15 0 20 20 25 30
4+
10 5 30 15 0 20
5+
-
6+
0 30 15 10 20
7+
10 10 10 20 20
8+
25 30 15 35 40
9+
15 10 25 15 20

0 commit comments

Comments
 (0)