Maximum Points After Collecting Coins From All Nodes - Problem
Maximum Points After Collecting Coins From All Nodes

Imagine you're a treasure hunter exploring an ancient tree-shaped dungeon! ๐Ÿดโ€โ˜ ๏ธ

You're given an undirected tree with n nodes (labeled 0 to n-1), rooted at node 0. The tree structure is defined by a 2D array edges where edges[i] = [ai, bi] represents a connection between nodes ai and bi.

Each node contains a treasure chest with coins[i] coins, and you have a magical parameter k. Here's the catch: you can only collect coins from a node after collecting from all its ancestors!

When collecting coins from node i, you have two magical choices:
1. Direct Collection: Get coins[i] - k points (can be negative!)
2. Halving Spell: Get floor(coins[i] / 2) points, but this spell affects the entire subtree - all coins in nodes below get halved too!

Goal: Return the maximum points you can collect from all nodes in the tree.

Input & Output

example_1.py โ€” Basic Tree
$ Input: edges = [[0,1],[1,2],[1,3]], coins = [1,4,2,3], k = 2
โ€บ Output: 7
๐Ÿ’ก Note: At node 0: direct collection gives 1-2=-1. At node 1: halving gives 4//2=2 and halves subtree. Node 2 becomes 2//2=1, node 3 becomes 3//2=1. Collecting: 1-2=-1 + 2 + (1-2) + (1-2) = -1+2-1-1 = -1. Better: Use halving at root level strategically to maximize total points = 7.
example_2.py โ€” Single Node
$ Input: edges = [], coins = [10], k = 3
โ€บ Output: 7
๐Ÿ’ก Note: Only one node. Direct collection: 10-3=7. Halving: 10//2=5. Direct collection is better, so answer is 7.
example_3.py โ€” Large K Value
$ Input: edges = [[0,1]], coins = [2,1], k = 5
โ€บ Output: -2
๐Ÿ’ก Note: Node 0: direct=2-5=-3, halving=2//2=1. Node 1: direct=1-5=-4, halving=1//2=0. If we halve at node 0, node 1 becomes 1//2=0, so halving at node 1 gives 0. Best strategy gives -2 total points.

Constraints

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

Visualization

Tap to expand
Root๐Ÿ’ฐ 100 coinsChild 1๐Ÿ’ฐ 50Child 2๐Ÿ’ฐ 60Leaf๐Ÿ’ฐ 30Leaf๐Ÿ’ฐ 40Decision at Each Node:Option 1: Direct โ†’ coins[i] - kOption 2: Halving โ†’ coins[i] / 2 (affects entire subtree)โšก Halving propagates down!Memoization Magic:Cache: memo[node][halving_count]States: โ‰ค 14 halving levelsTime: O(n ร— 14) = O(n)๐Ÿง  Avoid recomputation!
Understanding the Visualization
1
Start at Root
Begin at the root node (treasure chamber entrance). You must decide: direct collection or halving spell?
2
Decision Impact
Direct collection gets coins[i]-k points. Halving spell gets coins[i]/2 but halves all treasures in the subtree.
3
Recursive Choices
Continue this decision process for every node in the tree, considering the cumulative effect of previous halving spells.
4
Memoization
Cache results for (node, halving_count) pairs since the same subtree state can be reached multiple ways.
Key Takeaway
๐ŸŽฏ Key Insight: The halving operation can only be applied a limited number of times (โ‰ค14) before all coin values become 0, making memoization highly effective with bounded state space.
Asked in
Google 42 Meta 38 Amazon 35 Microsoft 29
54.0K Views
Medium Frequency
~35 min Avg. Time
1.8K 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