Skip to content

2538. Difference Between Maximum and Minimum Price Sum 👍

  • Time: $O(n)$
  • Space: $O(n)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class Solution {
 public:
  long long maxOutput(int n, vector<vector<int>>& edges, vector<int>& price) {
    int ans = 0;
    vector<vector<int>> tree(n);
    // maxSums[i] := the maximum the sum of path rooted at i
    vector<int> maxSums(n);

    for (const vector<int>& edge : edges) {
      const int u = edge[0];
      const int v = edge[1];
      tree[u].push_back(v);
      tree[v].push_back(u);
    }

    // Precalculate `maxSums`.
    maxSum(tree, 0, /*prev=*/-1, maxSums, price);
    reroot(tree, 0, /*prev=*/-1, /*parentSum=*/0, maxSums, price, ans);
    return ans;
  }

 private:
  int maxSum(const vector<vector<int>>& tree, int u, int prev,
             vector<int>& maxSums, const vector<int>& price) {
    int maxChildSum = 0;
    for (const int v : tree[u])
      if (v != prev)
        maxChildSum = max(maxChildSum, maxSum(tree, v, u, maxSums, price));
    return maxSums[u] = price[u] + maxChildSum;
  }

  void reroot(const vector<vector<int>>& tree, int u, int prev, int parentSum,
              const vector<int>& maxSums, const vector<int>& price, int& ans) {
    // Get the top two subtree sums and the top one node index.
    int maxSubtreeSum1 = 0;
    int maxSubtreeSum2 = 0;
    int maxNode = -1;
    for (const int v : tree[u]) {
      if (v == prev)
        continue;
      if (maxSums[v] > maxSubtreeSum1) {
        maxSubtreeSum2 = maxSubtreeSum1;
        maxSubtreeSum1 = maxSums[v];
        maxNode = v;
      } else if (maxSums[v] > maxSubtreeSum2) {
        maxSubtreeSum2 = maxSums[v];
      }
    }

    if (tree[u].size() == 1)
      ans = max({ans, parentSum, maxSubtreeSum1});

    for (const int v : tree[u]) {
      if (v == prev)
        continue;
      const int nextParentSum =
          (v == maxNode ? price[u] + max(parentSum, maxSubtreeSum2)
                        : price[u] + max(parentSum, maxSubtreeSum1));
      reroot(tree, v, u, nextParentSum, maxSums, price, ans);
    }
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class Solution {
  public long maxOutput(int n, int[][] edges, int[] price) {
    List<Integer>[] tree = new List[n];
    // maxSums[i] := the maximum the sum of path rooted at i
    int[] maxSums = new int[n];

    for (int i = 0; i < n; ++i)
      tree[i] = new ArrayList<>();

    for (int[] edge : edges) {
      final int u = edge[0];
      final int v = edge[1];
      tree[u].add(v);
      tree[v].add(u);
    }

    // Precalculate `maxSums`.
    maxSum(tree, 0, /*prev=*/-1, maxSums, price);
    reroot(tree, 0, /*prev=*/-1, /*parentSum=*/0, maxSums, price);
    return (long) ans;
  }

  private int ans = 0;

  private int maxSum(List<Integer>[] tree, int u, int prev, int[] maxSums, int[] price) {
    int maxChildSum = 0;
    for (final int v : tree[u])
      if (v != prev)
        maxChildSum = Math.max(maxChildSum, maxSum(tree, v, u, maxSums, price));
    return maxSums[u] = price[u] + maxChildSum;
  }

  private void reroot(List<Integer>[] tree, int u, int prev, int parentSum, int[] maxSums,
                      int[] price) {
    // Get top two sums and top one node index.
    int maxSubtreeSum1 = 0;
    int maxSubtreeSum2 = 0;
    int maxNode = -1;
    for (final int v : tree[u]) {
      if (v == prev)
        continue;
      if (maxSums[v] > maxSubtreeSum1) {
        maxSubtreeSum2 = maxSubtreeSum1;
        maxSubtreeSum1 = maxSums[v];
        maxNode = v;
      } else if (maxSums[v] > maxSubtreeSum2) {
        maxSubtreeSum2 = maxSums[v];
      }
    }

    if (tree[u].size() == 1)
      ans = Math.max(ans, Math.max(parentSum, maxSubtreeSum1));

    for (final int v : tree[u]) {
      if (v == prev)
        continue;
      final int nextParentSum = (v == maxNode ? price[u] + Math.max(parentSum, maxSubtreeSum2)
                                              : price[u] + Math.max(parentSum, maxSubtreeSum1));
      reroot(tree, v, u, nextParentSum, maxSums, price);
    }
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class Solution:
  def maxOutput(self, n: int, edges: list[list[int]], price: list[int]) -> int:
    ans = 0
    tree = [[] for _ in range(n)]
    maxSums = [0] * n  # maxSums[i] := the maximum the sum of path rooted at i

    for u, v in edges:
      tree[u].append(v)
      tree[v].append(u)

    def maxSum(u: int, prev: int) -> int:
      maxChildSum = 0
      for v in tree[u]:
        if v != prev:
          maxChildSum = max(maxChildSum, maxSum(v, u))
      maxSums[u] = price[u] + maxChildSum
      return maxSums[u]

    # Precalculate `maxSums`.
    maxSum(0, -1)

    def reroot(u: int, prev: int, parentSum: int) -> None:
      nonlocal ans
      # Get the top two subtree sums and the top one node index.
      maxSubtreeSum1 = 0
      maxSubtreeSum2 = 0
      maxNode = -1
      for v in tree[u]:
        if v == prev:
          continue
        if maxSums[v] > maxSubtreeSum1:
          maxSubtreeSum2 = maxSubtreeSum1
          maxSubtreeSum1 = maxSums[v]
          maxNode = v
        elif maxSums[v] > maxSubtreeSum2:
          maxSubtreeSum2 = maxSums[v]

      if len(tree[u]) == 1:
        ans = max(ans, parentSum, maxSubtreeSum1)

      for v in tree[u]:
        if v == prev:
          continue
        nextParentSum = (
            price[u] + max(parentSum, maxSubtreeSum2) if v == maxNode else
            price[u] + max(parentSum, maxSubtreeSum1))
        reroot(v, u, nextParentSum)

    reroot(0, -1, 0)
    return ans