public class Solution {
public int minMoves(int[] a) {
//incrementing n-1 elements to reach max is equivalent to
//decrementing max elemnt by (max-min)) to reach the rest
int count = 0;
boolean proceed = true;
while(proceed){
int max = a[0];
int min = a[0];
int idx = 0;
proceed = false;
for(int i=1; i < a.length; i++){
if(max < a[i]){
max = a[i];
idx = i;
proceed = true;
}
if(a[i] < min){
min = a[i];
}
}
a[idx] -= (max-min);
count += (max-min);
}
return count;
}
}

I think the core idea is to find all the possible solutions counted from the top to the current node. And the current_targets for current node are the pre_node_targets - pre_node.val.