Skip to content

2920. Maximum Points After Collecting Coins From All Nodes 👍

  • Time: $O(\log (10^4) \cdot n) = 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
class Solution {
 public:
  int maximumPoints(vector<vector<int>>& edges, vector<int>& coins, int k) {
    const int n = coins.size();
    vector<vector<int>> graph(n);
    vector<vector<int>> mem(n, vector<int>(kMaxHalved + 1, -1));

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

    return dfs(graph, 0, /*prev=*/-1, coins, k, /*halved=*/0, mem);
  }

 private:
  static constexpr int kMaxCoin = 10000;
  static constexpr int kMaxHalved = 13;  // log2(kMaxCoin) = 13

  int dfs(const vector<vector<int>>& graph, int u, int prev,
          const vector<int>& coins, int k, int halved,
          vector<vector<int>>& mem) {
    // All the children will be 0, so no need to explore.
    if (halved > kMaxHalved)
      return 0;
    if (mem[u][halved] != -1)
      return mem[u][halved];

    const int val = coins[u] / (1 << halved);
    int takeAll = val - k;
    int takeHalf = floor(val / 2.0);

    for (const int v : graph[u]) {
      if (v == prev)
        continue;
      takeAll += dfs(graph, v, u, coins, k, halved, mem);
      takeHalf += dfs(graph, v, u, coins, k, halved + 1, mem);
    }

    return mem[u][halved] = max(takeAll, takeHalf);
  }
};
 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
class Solution {
  public int maximumPoints(int[][] edges, int[] coins, int k) {
    final int n = coins.length;
    List<Integer>[] graph = new List[n];

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

    Integer[][] mem = new Integer[n][kMaxHalved + 1];

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

    return dfs(graph, 0, /*prev=*/-1, coins, k, /*halved=*/0, mem);
  }

  private static final int kMaxCoin = 10000;
  private static final int kMaxHalved = (int) (Math.log(kMaxCoin) / Math.log(2)) + 1;

  private int dfs(List<Integer>[] graph, int u, int prev, int[] coins, int k, int halved,
                  Integer[][] mem) {
    // All the children will be 0, so no need to explore.
    if (halved > kMaxHalved)
      return 0;
    if (mem[u][halved] != null)
      return mem[u][halved];

    final int val = coins[u] / (1 << halved);
    int takeAll = val - k;
    int takeHalf = (int) Math.floor(val / 2.0);

    for (final int v : graph[u]) {
      if (v == prev)
        continue;
      takeAll += dfs(graph, v, u, coins, k, halved, mem);
      takeHalf += dfs(graph, v, u, coins, k, halved + 1, mem);
    }

    return mem[u][halved] = Math.max(takeAll, takeHalf);
  }
}
 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
class Solution:
  def maximumPoints(
      self,
      edges: list[list[int]],
      coins: list[int],
      k: int,
  ) -> int:
    kMaxCoin = 10000
    kMaxHalved = int(kMaxCoin).bit_length()
    n = len(coins)
    graph = [[] for _ in range(n)]

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

    @functools.lru_cache(None)
    def dfs(u: int, prev: int, halved: int) -> int:
      # All the children will be 0, so no need to explore.
      if halved > kMaxHalved:
        return 0

      val = coins[u] // (1 << halved)
      takeAll = val - k
      takeHalf = math.floor(val / 2)

      for v in graph[u]:
        if v == prev:
          continue
        takeAll += dfs(v, u, halved)
        takeHalf += dfs(v, u, halved + 1)

      return max(takeAll, takeHalf)

    return dfs(0, -1, 0)