Problem solution · Java

Minimum Score After Removals on a Tree

Minimum Score After Removals on a Tree: 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
79 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 Minimum Score After Removals on a Tree, 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

  • 79 lines of Java from the credited upstream file 2322.java.
  • The implementation visibly relies on sequence storage, hash lookup, ordered lookup.
  • 8 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 codeMinimum Score After Removals on a Tree · JavaJava
Use this to learn the idea, then write your own version.
class Solution {  public int minimumScore(int[] nums, int[][] edges) {    final int n = nums.length;    final int xors = getXors(nums);    int[] subXors = nums.clone();    List<Integer>[] tree = new List[n];    Set<Integer>[] children = new Set[n];     for (int i = 0; i < n; ++i)      tree[i] = new ArrayList<>();     for (int i = 0; i < n; ++i)      children[i] = new HashSet<>(Arrays.asList(i));     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, -1, subXors, children);     int ans = Integer.MAX_VALUE;     for (int i = 0; i < edges.length; ++i) {      int a = edges[i][0];      int b = edges[i][1];      if (children[a].contains(b)) {        final int temp = a;        a = b;        b = a;      }      for (int j = 0; j < i; ++j) {        int c = edges[j][0];        int d = edges[j][1];        if (children[c].contains(d)) {          final int temp = c;          c = d;          d = temp;        }        int[] cands;        if (a != c && children[a].contains(c))          cands = new int[] {subXors[c], subXors[a] ^ subXors[c], xors ^ subXors[a]};        else if (a != c && children[c].contains(a))          cands = new int[] {subXors[a], subXors[c] ^ subXors[a], xors ^ subXors[c]};        else          cands = new int[] {subXors[a], subXors[c], xors ^ subXors[a] ^ subXors[c]};        ans = Math.min(ans, Arrays.stream(cands).max().getAsInt() -                                Arrays.stream(cands).min().getAsInt());      }    }     return ans;  }   private Pair<Integer, Set<Integer>> dfs(List<Integer>[] tree, int u, int prev, int[] subXors,                                          Set<Integer>[] children) {    for (final int v : tree[u]) {      if (v == prev)        continue;      final Pair<Integer, Set<Integer>> pair = dfs(tree, v, u, subXors, children);      final int vXor = pair.getKey();      final Set<Integer> vChildren = pair.getValue();      subXors[u] ^= vXor;      for (final int child : vChildren)        children[u].add(child);    }    return new Pair<>(subXors[u], children[u]);  }   private int getXors(int[] nums) {    int xors = 0;    for (final int num : nums)      xors ^= num;    return xors;  }} 

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 ↗