-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2467 Most Profitable Path in a Tree.js
More file actions
64 lines (62 loc) · 1.97 KB
/
2467 Most Profitable Path in a Tree.js
File metadata and controls
64 lines (62 loc) · 1.97 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
/**
* @param {number[][]} edges
* @param {number} bob
* @param {number[]} amount
* @return {number}
*/
var mostProfitablePath = function (edges, bob, amount) {
const n = amount.length;
const graph = Array.from({ length: n }, () => []);
// Step 1: Construct adjacency list for the tree
for (const [u, v] of edges) {
graph[u].push(v);
graph[v].push(u);
}
// Step 2: Find the exact path of Bob from `bob` to `0`
let bobTime = Array(n).fill(Infinity);
let bobPath = new Map();
function findBobPath(node, parent, depth) {
bobPath.set(node, depth);
if (node === 0) return true;
for (const neighbor of graph[node]) {
if (neighbor !== parent && findBobPath(neighbor, node, depth + 1)) {
return true;
}
}
bobPath.delete(node);
return false;
}
findBobPath(bob, -1, 0);
// Mark Bob's reach time from his path
bobTime = Array(n).fill(Infinity);
let time = 0;
for (const [node, t] of bobPath.entries()) {
bobTime[node] = t;
}
let maxIncome = -Infinity;
// Step 3: DFS for Alice to find the most profitable path
function dfsAlice(node, parent, currTime, income) {
// Case 1: Alice reaches first -> Full reward
if (currTime < bobTime[node]) {
income += amount[node];
}
// Case 2: Both reach at the same time -> Half reward
else if (currTime === bobTime[node]) {
income += amount[node] / 2;
}
// Case 3: Bob reaches first -> No reward
let isLeaf = true;
for (const neighbor of graph[node]) {
if (neighbor !== parent) {
isLeaf = false;
dfsAlice(neighbor, node, currTime + 1, income);
}
}
// If it's a leaf, update maxIncome
if (isLeaf) {
maxIncome = Math.max(maxIncome, income);
}
}
dfsAlice(0, -1, 0, 0);
return maxIncome;
};