Find Number of Coins to Place in Tree Nodes Problem
Description
LeetCode Problem 2973.
You are given an undirected tree with n nodes labeled from 0 to n - 1, and rooted at node 0. You are given a 2D integer array edges of length n - 1, where edges[i] = [a_i, b_i] indicates that there is an edge between nodes a_i and b_i in the tree.
You are also given a 0-indexed integer array cost of length n, where cost[i] is the cost assigned to the i^th 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
iis less than3, place1coin. - Otherwise, place an amount of coins equal to the maximum product of cost values assigned to
3distinct nodes in the subtree of nodei. If this product is negative, place0coins.
Return an array coin of size n such that coin[i] is the number of coins placed at node i.
Example 1:

Input: edges = [[0,1],[0,2],[0,3],[0,4],[0,5]], cost = [1,2,3,4,5,6]
Output: [120,1,1,1,1,1]
Explanation: For node 0 place 6 * 5 * 4 = 120 coins. All other nodes are leaves with subtree of size 1, place 1 coin on each of them.
Example 2:

Input: edges = [[0,1],[0,2],[1,3],[1,4],[1,5],[2,6],[2,7],[2,8]], cost = [1,4,2,3,5,7,8,-4,2]
Output: [280,140,32,1,1,1,1,1,1]
Explanation: The coins placed on each node are:
- Place 8 * 7 * 5 = 280 coins on node 0.
- Place 7 * 5 * 4 = 140 coins on node 1.
- Place 8 * 2 * 2 = 32 coins on node 2.
- All other nodes are leaves with subtree of size 1, place 1 coin on each of them.
Example 3:

Input: edges = [[0,1],[0,2]], cost = [1,2,-2]
Output: [0,1,1]
Explanation: Node 1 and 2 are leaves with subtree of size 1, place 1 coin on each of them. For node 0 the only possible product of cost is 2 * 1 * -2 = -4. Hence place 0 coins on node 0.
Constraints:
2 <= n <= 2 * 10^4edges.length == n - 1edges[i].length == 20 <= a_i, b_i < ncost.length == n1 <= |cost[i]| <= 10^4- The input is generated such that
edgesrepresents a valid tree.
Solution
Approach: DFS + Sorting
For a node a, the number of coins depends on its subtree: with fewer than 3 nodes the answer is 1; otherwise it is the largest product of three costs from the subtree, or 0 if that product is negative.
The best product uses either the three largest costs, or the two smallest (most negative) costs and the largest one. So each subtree only needs to remember its 2 smallest and 3 largest costs.
Build an adjacency list and run dfs(a, parent), which returns that short list for the subtree of a. It collects cost[a] and the lists returned by the children, sorts them, and, if there are at least 3 values, sets ans[a] to max(0, res[m-1] * res[m-2] * res[m-3], res[0] * res[1] * res[m-1]). If the list has more than 5 values, only the first 2 and last 3 are kept and returned.
The time complexity is O(n log n) (each sort has at most about 5 items per child) and the space complexity is O(n), where n is the number of nodes.
class Solution:
def placedCoins(self, edges: List[List[int]], cost: List[int]) -> List[int]:
def dfs(a: int, fa: int) -> List[int]:
res = [cost[a]]
for b in g[a]:
if b != fa:
res.extend(dfs(b, a))
res.sort()
if len(res) >= 3:
ans[a] = max(res[-3] * res[-2] * res[-1], res[0] * res[1] * res[-1], 0)
if len(res) > 5:
res = res[:2] + res[-3:]
return res
n = len(cost)
g = [[] for _ in range(n)]
for a, b in edges:
g[a].append(b)
g[b].append(a)
ans = [1] * n
dfs(0, -1)
return ans