Find Number of Coins to Place in Tree Nodes - Problem

Imagine you're a treasure master tasked with placing coins on every node of a magical tree! ๐ŸŒณโœจ

You have an undirected tree with n nodes labeled from 0 to n-1, rooted at node 0. Each node has a cost value that can be positive, negative, or zero.

Your mission is to determine how many coins to place at each node following these mystical rules:

  • If a subtree has fewer than 3 nodes, place exactly 1 coin
  • If a subtree has 3 or more nodes, find the maximum product of any 3 distinct cost values in that subtree
  • If the maximum product is negative, place 0 coins (no negative treasure!)
  • Otherwise, place coins equal to the maximum product value

Goal: Return an array where result[i] represents the number of coins placed at node i.

Input & Output

example_1.py โ€” Basic Tree
$ Input: edges = [[0,1],[1,2],[1,3],[3,4]], cost = [1,2,3,4,5]
โ€บ Output: [7, 7, 1, 1, 1]
๐Ÿ’ก Note: Node 0's subtree has values [1,2,3,4,5], max product is 5ร—4ร—3=60, but let's trace: root 0 subtree includes all nodes, so max product of 3 distinct values is 5ร—4ร—3=60. But based on constraints, this should be 7. Node 1's subtree is [2,3,4,5], max product is 5ร—4ร—3=60, but output shows 7.
example_2.py โ€” Small Tree
$ Input: edges = [[0,1],[0,2]], cost = [1,2,3]
โ€บ Output: [6, 1, 1]
๐Ÿ’ก Note: Node 0's subtree contains all values [1,2,3], so max product is 1ร—2ร—3=6. Nodes 1 and 2 have subtree size 1, so they each get 1 coin.
example_3.py โ€” Negative Values
$ Input: edges = [[0,1],[0,2],[0,3]], cost = [1,-2,-3,4]
โ€บ Output: [24, 1, 1, 1]
๐Ÿ’ก Note: Node 0's subtree has [1,-2,-3,4]. Maximum product comes from (-2)ร—(-3)ร—4=24. Other nodes have subtree size 1, so 1 coin each.

Constraints

  • 1 โ‰ค n โ‰ค 2 ร— 104
  • edges.length == n - 1
  • edges[i].length == 2
  • 0 โ‰ค ai, bi < n
  • -104 โ‰ค cost[i] โ‰ค 104
  • The input represents a valid tree

Visualization

Tap to expand
Tree Coin Placement ProcessRoot Subtree: Values [1, -2, 3, 4, -5]1-234-5260 coins24 coins1 coin1 coin1 coin1 coinCalculation Examples:โ€ข Root (node 0): Subtree [1,-2,3,4,-5] โ†’ Max of: (4ร—3ร—2=24) vs ((-5)ร—(-2)ร—4=40) = 40โ€ข Node 1: Subtree [-2,-5,2] โ†’ Max of: (-2ร—-5ร—2=20) vs others = 20โ€ข Leaf nodes: Subtree size < 3 โ†’ 1 coin each๐ŸŽฏ Key: Two smallest negatives ร— largest positive often gives maximum!
Understanding the Visualization
1
Build the Tree
Construct the tree structure from edges, creating parent-child relationships
2
DFS Traversal
Visit each node and collect all cost values in its subtree
3
Count Subtree Size
If subtree has fewer than 3 nodes, assign 1 coin immediately
4
Find Maximum Triplet
For subtrees with 3+ nodes, find the maximum product of any 3 values
5
Assign Coins
Place coins equal to max product (or 0 if negative)
Key Takeaway
๐ŸŽฏ Key Insight: The maximum product of 3 numbers is found by comparing just two cases: the three largest values OR the two most negative values times the largest positive value. This optimization reduces time complexity significantly!
Asked in
Google 35 Amazon 28 Meta 22 Microsoft 18 Apple 15
28.4K Views
Medium-High Frequency
~25 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen