-
Notifications
You must be signed in to change notification settings - Fork 19
/
47_礼物的最大价值.py
53 lines (43 loc) · 1.5 KB
/
47_礼物的最大价值.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
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
def get_max_value1(matrix):
if not isinstance(matrix, list) or len(matrix) == 0 or \
not isinstance(matrix[0], list) or len(matrix[0]) == 0:
return
num_row = len(matrix)
num_col = len(matrix[0])
for row in matrix:
if not isinstance(row, list) or len(row) != num_col:
return
max_value_matrix = [[0] * num_col for _ in range(num_row)]
for i in range(num_row):
for j in range(num_col):
up = 0
left = 0
if i > 0:
up = max_value_matrix[i - 1][j]
if j > 0:
left = max_value_matrix[i][j - 1]
max_value_matrix[i][j] = matrix[i][j] + max(up, left)
return max_value_matrix[-1][-1]
def get_max_value2(matrix):
if not isinstance(matrix, list) or len(matrix) == 0 or \
not isinstance(matrix[0], list) or len(matrix[0]) == 0:
return
num_row = len(matrix)
num_col = len(matrix[0])
for row in matrix:
if not isinstance(row, list) or len(row) != num_col:
return
max_values = [0] * num_col
for i in range(num_row):
for j in range(num_col):
up = 0
left = 0
if i > 0:
up = max_values[j]
if j > 0:
left = max_values[j - 1]
max_values[j] = matrix[i][j] + max(up, left)
return max_values[-1]
if __name__ == "__main__":
m = [[1, 10, 3, 8], [12, 2, 9, 6], [5, 7, 4, 11], [3, 7, 16, 5]]
print(get_max_value2(m))