-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathmaximum-score-after-applying-operations-on-a-tree.py
55 lines (50 loc) · 1.65 KB
/
maximum-score-after-applying-operations-on-a-tree.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
54
55
# Time: O(n)
# Space: O(n)
# iterative dfs, tree dp
class Solution(object):
def maximumScoreAfterOperations(self, edges, values):
"""
:type edges: List[List[int]]
:type values: List[int]
:rtype: int
"""
def iter_dfs():
dp = [0]*len(values)
stk = [(1, 0, -1)]
while stk:
step, u, p = stk.pop()
if step == 1:
if len(adj[u]) == (1 if u else 0):
dp[u] = values[u]
continue
stk.append((2, u, p))
for v in reversed(adj[u]):
if v != p:
stk.append((1, v, u))
elif step == 2:
dp[u] = min(sum(dp[v] for v in adj[u] if v != p), values[u]) # min(pick u, not pick u)
return dp[0]
adj = [[] for _ in xrange(len(values))]
for u, v in edges:
adj[u].append(v)
adj[v].append(u)
return sum(values)-iter_dfs()
# Time: O(n)
# Space: O(n)
# dfs, tree dp
class Solution2(object):
def maximumScoreAfterOperations(self, edges, values):
"""
:type edges: List[List[int]]
:type values: List[int]
:rtype: int
"""
def dfs(u, p):
if len(adj[u]) == (1 if u else 0):
return values[u]
return min(sum(dfs(v, u) for v in adj[u] if v != p), values[u]) # min(pick u, not pick u)
adj = [[] for _ in xrange(len(values))]
for u, v in edges:
adj[u].append(v)
adj[v].append(u)
return sum(values)-dfs(0, -1)