Problem solution · Java

Find Number of Coins to Place in Tree Nodes

Find Number of Coins to Place in Tree Nodes: a Java solution using depth-first search. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Depth-first search
Source
walkccc LeetCode Solutions
Length
68 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Depth-first search

For Find Number of Coins to Place in Tree Nodes, the implementation follows one branch at a time, making it suitable for components, trees, backtracking, or dependency exploration.

  1. Define the state carried into one recursive or stack frame.
  2. Mark or choose the current state before exploring children.
  3. Combine child results or undo the choice when the branch finishes.

Code notes

  • 68 lines of Java from the credited upstream file 2973.java.
  • The implementation visibly relies on sequence storage.
  • 3 loop blocks detected, together with recursive traversal.

Complexity

Count unique states for graph traversal; for backtracking, count the branching factor and maximum depth.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeFind Number of Coins to Place in Tree Nodes · JavaJava
Use this to learn the idea, then write your own version.
class ChildCost {  public ChildCost(int cost) {    if (cost > 0)      maxPosCosts.add(cost);    else      minNegCosts.add(cost);  }   public void update(ChildCost childCost) {    numNodes += childCost.numNodes;    maxPosCosts.addAll(childCost.maxPosCosts);    minNegCosts.addAll(childCost.minNegCosts);    maxPosCosts.sort(Comparator.reverseOrder());    minNegCosts.sort(Comparator.naturalOrder());    if (maxPosCosts.size() > 3)      maxPosCosts = maxPosCosts.subList(0, 3);    if (minNegCosts.size() > 2)      minNegCosts = minNegCosts.subList(0, 2);  }   public long maxProduct() {    if (numNodes < 3)      return 1;    if (maxPosCosts.isEmpty())      return 0;    long res = 0;    if (maxPosCosts.size() == 3)      res = (long) maxPosCosts.get(0) * maxPosCosts.get(1) * maxPosCosts.get(2);    if (minNegCosts.size() == 2)      res = Math.max(res, (long) minNegCosts.get(0) * minNegCosts.get(1) * maxPosCosts.get(0));    return res;  }   private int numNodes = 1;  private List<Integer> maxPosCosts = new ArrayList<>();  private List<Integer> minNegCosts = new ArrayList<>();} class Solution {  public long[] placedCoins(int[][] edges, int[] cost) {    final int n = cost.length;    long[] ans = new long[n];    List<Integer>[] tree = new List[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);    }     dfs(tree, 0, /*prev=*/-1, cost, ans);    return ans;  }   private ChildCost dfs(List<Integer>[] tree, int u, int prev, int[] cost, long[] ans) {    ChildCost res = new ChildCost(cost[u]);    for (final int v : tree[u])      if (v != prev)        res.update(dfs(tree, v, u, cost, ans));    ans[u] = res.maxProduct();    return res;  }} 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗