Difference Between Maximum and Minimum Price Sum - Problem

Imagine you're a network architect tasked with optimizing the cost efficiency of a tree-structured network with n nodes (indexed 0 to n-1). You have an unrooted tree defined by edges array, where each edge connects two nodes, and each node has an associated operational price.

The price sum of any path is simply the sum of prices of all nodes along that path. Here's the challenge: you can choose any node as the root of your network. Once you pick a root, you need to consider all possible paths that start from this root node.

The incurred cost for choosing a particular root is the difference between the maximum and minimum price sum among all paths starting from that root. Your goal is to find the maximum possible cost you can achieve by choosing the optimal root.

This problem combines tree traversal, dynamic programming, and optimal substructure - making it a fascinating challenge that tests your ability to think about tree rerooting and path optimization!

Input & Output

example_1.py โ€” Basic Tree
$ Input: n = 4, edges = [[0,1],[1,2],[1,3]], price = [2,3,6,5]
โ€บ Output: 6
๐Ÿ’ก Note: When root is node 1, paths are: [1]โ†’[0] (sum=5), [1]โ†’[2] (sum=9), [1]โ†’[3] (sum=8). Max=9, Min=3, difference=6. This is optimal.
example_2.py โ€” Linear Chain
$ Input: n = 3, edges = [[0,1],[1,2]], price = [1,4,2]
โ€บ Output: 5
๐Ÿ’ก Note: When root is node 1, paths are: [1]โ†’[0] (sum=5) and [1]โ†’[2] (sum=6). Difference = 6-5 = 1. When root is 0 or 2, we get larger differences.
example_3.py โ€” Single Node
$ Input: n = 1, edges = [], price = [5]
โ€บ Output: 0
๐Ÿ’ก Note: Only one node exists, so there's only one path with sum=5. Max=Min=5, so difference=0.

Constraints

  • 1 โ‰ค n โ‰ค 2 ร— 104
  • edges.length == n - 1
  • 0 โ‰ค ai, bi โ‰ค n - 1
  • edges represents a valid tree
  • 1 โ‰ค price[i] โ‰ค 105
  • The graph is guaranteed to be a connected tree

Visualization

Tap to expand
Tree Rerooting for Optimal CostRoot = 00$21$32$6Path: 0โ†’1Sum: 5Path: 0โ†’2Sum: 8Cost: 8-5 = 3Root = 11$30$22$6Path: 1โ†’0Sum: 5Path: 1โ†’2Sum: 9Cost: 9-5 = 4Root = 22$61$30$2Path: 2โ†’1โ†’0Sum: 11Cost: 11-6 = 5๐ŸŽฏ Optimal SolutionRoot = 2 gives maximum cost = 5Use DP with rerooting to find this efficiently!
Understanding the Visualization
1
Original Tree
Start with an unrooted tree where each node has a price
2
Root Choice 1
Choose node 0 as root and calculate all path sums from this root
3
Root Choice 2
Choose node 1 as root and recalculate - notice how paths change
4
Optimize
Use DP to avoid recalculating everything when changing roots
5
Final Answer
Find the root that gives maximum difference between max and min paths
Key Takeaway
๐ŸŽฏ Key Insight: Different root choices create different path structures. The optimal approach uses dynamic programming with tree rerooting to efficiently explore all possibilities in O(n) time rather than the naive O(nยฒ) approach.
Asked in
Google 23 Meta 18 Amazon 15 Microsoft 12
27.5K Views
Medium Frequency
~35 min Avg. Time
892 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