Opening your workspace…
Workspace/Coding room
Opening your workspace…
Choose one node for the brewery delivery hub. The link between node i and parent[i] has length lengths[i] and can be traveled in either direction. Each node needs demand[i] deliveries. A hub costs the sum, over all nodes, of demand[i] times the total link length on the unique path from the hub to i. Return [hubIndex, minimumCost]. If several nodes have the same minimum cost, choose the smallest index. A delivery to the hub itself has distance zero. The tree has nodes numbered 0 through n - 1. parent[0] is -1; every other parent[i] is the parent of node i. The tree is connected and has no cycles. Node numbers do not imply traversal order: a parent may have a larger number than its child.
[-1,0,0], [0,2,1], [0,3,1]OUTPUT[1,3]At node 1, only the delivery to node 2 travels: its path length is 2 + 1 = 3. Hub 0 costs 7 and hub 2 costs 9.
[-1,0], [0,4], [2,2]OUTPUT[0,8]Either endpoint costs 2 times 4 = 8, so choose the smaller index, 0.
Tab inserts two spaces. Press Escape, then Tab, to leave the editor. Press Control or Command and Enter to submit.
[-1,0,0], [0,2,1], [0,3,1][1,3]Public samples only. These checks do not award ranked points.
Walk through a solution, check its time and space costs, and compare it with yours.
Checking your progress…