10034. Find Number of Coins to Place in Tree Nodes
You are given an undirected tree with
n
nodes labeled from0
ton - 1
, and rooted at node0
. You are given a 2D integer arrayedges
of lengthn - 1
, whereedges[i] = [ai, bi]
indicates that there is an edge between nodesai
andbi
in the tree.You are also given a 0-indexed integer array
cost
of lengthn
, wherecost[i]
is the cost assigned to theith
node.You need to place some coins on every node of the tree. The number of coins to be placed at node
i
can be calculated as:
- If size of the subtree of node
i
is less than3
, place1
coin.- Otherwise, place an amount of coins equal to the maximum product of cost values assigned to
3
distinct nodes in the subtree of nodei
. If this product is negative, place0
coins.Return an array
coin
of sizen
such thatcoin[i]
is the number of coins placed at nodei
.
c++
1 |
|