forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
difference-between-maximum-and-minimum-price-sum.py
177 lines (164 loc) · 5.34 KB
/
difference-between-maximum-and-minimum-price-sum.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# Time: O(n)
# Space: O(n)
# iterative dfs, tree dp
class Solution(object):
def maxOutput(self, n, edges, price):
"""
:type n: int
:type edges: List[List[int]]
:type price: List[int]
:rtype: int
"""
def iter_dfs():
result = 0
stk = [(1, (0, -1, [price[0], 0]))]
while stk:
step, args = stk.pop()
if step == 1:
u, p, ret = args
stk.append((2, (u, p, ret, 0)))
elif step == 2:
u, p, ret, i = args
if i == len(adj[u]):
continue
stk.append((2, (u, p, ret, i+1)))
v = adj[u][i]
if v == p:
continue
new_ret = [price[v], 0] # [max_path_sum, max_path_sum_without_last_node]
stk.append((3, (u, new_ret, ret)))
stk.append((1, (v, u, new_ret)))
elif step == 3:
u, new_ret, ret = args
result = max(result, ret[0]+new_ret[1], ret[1]+new_ret[0])
ret[0] = max(ret[0], new_ret[0]+price[u])
ret[1] = max(ret[1], new_ret[1]+price[u])
return result
adj = [[] for _ in xrange(n)]
for u, v in edges:
adj[u].append(v)
adj[v].append(u)
return iter_dfs()
# Time: O(n)
# Space: O(n)
# dfs, tree dp
class Solution2(object):
def maxOutput(self, n, edges, price):
"""
:type n: int
:type edges: List[List[int]]
:type price: List[int]
:rtype: int
"""
def dfs(u, p):
dp = [price[u], 0] # [max_path_sum, max_path_sum_without_last_node]
for v in adj[u]:
if v == p:
continue
new_dp = dfs(v, u)
result[0] = max(result[0], dp[0]+new_dp[1], dp[1]+new_dp[0])
dp[0] = max(dp[0], new_dp[0]+price[u])
dp[1] = max(dp[1], new_dp[1]+price[u])
return dp
result = [0]
adj = [[] for _ in xrange(n)]
for u, v in edges:
adj[u].append(v)
adj[v].append(u)
dfs(0, -1)
return result[0]
# Time: O(n)
# Space: O(n)
# iterative dfs, tree dp
class Solution3(object):
def maxOutput(self, n, edges, price):
"""
:type n: int
:type edges: List[List[int]]
:type price: List[int]
:rtype: int
"""
def iter_dfs():
dp = [0]*n # max_sum
stk = [(1, 0, -1)]
while stk:
step, u, p = stk.pop()
if step == 1:
stk.append((2, u, p))
for v in adj[u]:
if v == p:
continue
stk.append((1, v, u))
elif step == 2:
dp[u] = price[u]
for v in adj[u]:
if v == p:
continue
dp[u] = max(dp[u], dp[v]+price[u])
return dp
def iter_dfs2():
result = 0
stk = [(0, -1, 0)]
while stk:
u, p, curr = stk.pop()
result = max(result, curr, dp[u]-price[u])
top2 = [[curr, p], [0, -1]]
for v in adj[u]:
if v == p:
continue
curr = [dp[v], v]
for i in xrange(len(top2)):
if curr > top2[i]:
top2[i], curr = curr, top2[i]
for v in adj[u]:
if v == p:
continue
stk.append((v, u, (top2[0][0] if top2[0][1] != v else top2[1][0])+price[u]))
return result
adj = [[] for _ in xrange(n)]
for u, v in edges:
adj[u].append(v)
adj[v].append(u)
dp = iter_dfs()
return iter_dfs2()
# Time: O(n)
# Space: O(n)
# dfs, tree dp
class Solution4(object):
def maxOutput(self, n, edges, price):
"""
:type n: int
:type edges: List[List[int]]
:type price: List[int]
:rtype: int
"""
def dfs(u, p):
dp[u] = price[u]
for v in adj[u]:
if v == p:
continue
dp[u] = max(dp[u], dfs(v, u)+price[u])
return dp[u]
def dfs2(u, p, curr):
result[0] = max(result[0], curr, dp[u]-price[u])
top2 = [[curr, p], [0, -1]]
for v in adj[u]:
if v == p:
continue
curr = [dp[v], v]
for i in xrange(len(top2)):
if curr > top2[i]:
top2[i], curr = curr, top2[i]
for v in adj[u]:
if v == p:
continue
dfs2(v, u, (top2[0][0] if top2[0][1] != v else top2[1][0])+price[u])
result = [0]
dp = [0]*n # max_sum
adj = [[] for _ in xrange(n)]
for u, v in edges:
adj[u].append(v)
adj[v].append(u)
dfs(0, -1)
dfs2(0, -1, 0)
return result[0]