Skip to content

Commit 8cbc1cd

Browse files
committed
Feb 24
1 parent 7a6ba0f commit 8cbc1cd

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from collections import defaultdict
2+
from typing import List
3+
4+
5+
class Solution:
6+
def mostProfitablePath(self, edges: List[List[int]], bob: int, amount: List[int]) -> int:
7+
graph = defaultdict(list)
8+
for u, v in edges:
9+
graph[u].append(v)
10+
graph[v].append(u)
11+
12+
def dfs_bob(node: int) -> List[int]:
13+
if node == 0:
14+
return [node]
15+
nonlocal visited
16+
visited.add(node)
17+
path = []
18+
for neighbour in graph[node]:
19+
if neighbour not in visited:
20+
path = dfs_bob(neighbour)
21+
if path:
22+
return [node] + path
23+
visited = set()
24+
bob_path = dfs_bob(bob)
25+
bob_visited_times = {node: i for i, node in enumerate(bob_path)}
26+
27+
def dfs_alice(node: int, time: int) -> int:
28+
max_amount = float('-inf')
29+
nonlocal visited
30+
visited.add(node)
31+
for neighbour in graph[node]:
32+
if neighbour not in visited:
33+
max_amount = max(max_amount, dfs_alice(neighbour, time+1))
34+
vertex_income = amount[node]
35+
if bob_visited_times.get(node, float('inf')) < time:
36+
vertex_income = 0
37+
elif bob_visited_times.get(node, float('inf')) == time:
38+
vertex_income = amount[node] // 2
39+
if max_amount == float('-inf'):
40+
return vertex_income
41+
return max_amount + vertex_income
42+
43+
visited = set()
44+
return dfs_alice(0, 0)
45+
46+
47+
def main():
48+
edges = [[0, 1], [1, 2], [1, 3], [3, 4]]
49+
bob = 3
50+
amount = [-2, 4, 2, -4, 6]
51+
assert Solution().mostProfitablePath(edges, bob, amount) == 6
52+
53+
edges = [[0, 1]]
54+
bob = 1
55+
amount = [-7280, 2350]
56+
assert Solution().mostProfitablePath(edges, bob, amount) == -7280
57+
58+
59+
if __name__ == '__main__':
60+
main()

2025-02-February-LeetCoding-Challenge/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
| February 21 | [1261. Find Elements in a Contaminated Binary Tree](https://leetcode.com/problems/find-elements-in-a-contaminated-binary-tree/) | Medium | Solved |
2828
| February 22 | [1028. Recover a Tree From Preorder Traversal](https://leetcode.com/problems/recover-a-tree-from-preorder-traversal/) | Hard | Solved |
2929
| February 23 | [889. Construct Binary Tree from Preorder and Postorder Traversal](https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/) | Medium | Unsolved |
30-
| February 24 | []() | | |
30+
| February 24 | [2467. Most Profitable Path in a Tree](https://leetcode.com/problems/most-profitable-path-in-a-tree/) | Medium | Unsolved |
3131
| February 25 | []() | | |
3232
| February 26 | []() | | |
3333
| February 27 | []() | | |
@@ -38,5 +38,5 @@
3838
| Level | Problems | Solved | Unsolved |
3939
| --- | --- | --- | --- |
4040
| Easy | 6 | 6 | 0 |
41-
| Medium | 15 | 12 | 3 |
41+
| Medium | 16 | 12 | 4 |
4242
| Hard | 1 | 1 | 0 |

0 commit comments

Comments
 (0)