FastPrepMinimum-Cost Root-to-Leaf Path in an N-ary Tree

Minimum-Cost Root-to-Leaf Path in an N-ary Tree

Bloomberg LP logoBloomberg LP● MediumNEW GRADPHONE SCREEN
Learn

Problem statement

Node i has value values[i] and child indices children[i]; root is 0. Path cost is the sum of node values from root through a leaf.

Return a long array containing cost first, followed by the node indices of a minimum-cost root-to-leaf path. Break cost ties lexicographically by index sequence.

Function

minimumCostPath(values: int[], children: int[][]) → long[]

Examples

Example 1

values = [5,2,3,1,4]children = [[1,2],[3,4],[],[],[]]return = [8,0,1,3]

Path 0,1,3 costs 5+2+1=8, less than the other leaves.

Constraints

  • 1 <= values.length == children.length <= 10^5.
  • Children form a rooted tree.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public long[] minimumCostPath(int[] values, int[][] children) {
  // Write your code here.
}
values[5,2,3,1,4]
children[[1,2],[3,4],[],[],[]]
expected[8,0,1,3]
Checking account…