forked from everbird/leetcode-py
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtriangle.py
More file actions
64 lines (47 loc) · 1.17 KB
/
triangle.py
File metadata and controls
64 lines (47 loc) · 1.17 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def run():
#input_data = [
# [2],
# [3,4],
# [6,5,7],
# [4,1,8,3],
# ]
input_data = [
[-1],
[2,3],
[1,-1,-3]
]
r = get_path(input_data)
assert r==-1, 'Failed'
print r
def get_path(data):
d = [[0] * len(data) for i in data]
last_line = data[-1]
d[-1] = last_line
get_min_sum(data, d, len(data) - 1)
print d
return d[0][0]
def get_min_sum(data, d, depth):
print d
if depth < 0:
return
if depth == len(data) - 1:
d[depth] = data[depth]
elif depth < len(data) - 1:
next_line = d[depth + 1] # ! d, not data
curret_line = data[depth]
for i in range(len(curret_line)):
a = next_line[i]
b = next_line[i + 1]
_i = i
if a > b:
_i = i + 1
d[depth][i] = data[depth][i] + d[depth + 1][_i]
print '>>>', data[depth][i], d[depth+1][_i], _i, a, b
if depth > 0:
get_min_sum(data, d, depth-1)
def main():
run()
if __name__ == '__main__':
main()