about summary refs log tree commit diff
path: root/users/wpcarro/scratch/data_structures_and_algorithms/trickling-water.py
blob: 45621990ecf94bf01f726c88ca2eb771ce74411b (plain) (blame)
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
class Node(object):
    def __init__(self, value, children=[]):
        self.value = value
        self.children = children


################################################################################
# Solution
################################################################################
def trip_time(node):
    s = []
    result = 0
    s.append((node.value, node))
    while s:
        p, node = s.pop()
        if not node.children:
            result = max(result, p)
        for x in node.children:
            s.append((p + x.value, x))
    return result


################################################################################
# Tests
################################################################################
tree = Node(
    0,
    children=[
        Node(5, children=[Node(6)]),
        Node(2, children=[
            Node(6),
            Node(10),
        ]),
        Node(3, children=[Node(2, children=[Node(11)])]),
    ])

assert trip_time(tree) == 16
print("Tests pass!")