Most Profitable Path in a Tree
specialist · 615 · lc medium +29 · verified · 67.3% accepted · 1,402 likes · top 73%
Description
There is an undirected tree with n nodes labeled 0 to n - 1, rooted at node 0. Edges are given by a 2D array edges.
Alice starts at node 0 (Bob's starting node is given). Bob moves from his starting node toward node 0 along the unique path, advancing one node per turn. Alice simultaneously moves from node 0 to any leaf, also one node per turn.
Each node i has an amount amount[i] (can be negative). The first person to reach a node collects the full amount; if both arrive simultaneously, each collects half. Alice wants to maximize her collected total, while Bob moves optimally (with no regard for Alice's score).
Return Alice's maximum possible net income.
Example 1:
Example 2:
Code