Sharing my solution here with explanation:

public class Solution { public int minDistance(int height, int width, int[] tree, int[] squirrel, int[][] nuts) { int[] dist2tree = new int[nuts.length], dist2squir = new int[nuts.length]; int maxDiff = Integer.MIN_VALUE, sum2tree = 0; for (int i = 0; i < nuts.length; i++) { dist2tree[i] = Math.abs(tree[0] - nuts[i][0]) + Math.abs(tree[1] - nuts[i][1]); sum2tree += dist2tree[i]; dist2squir[i] = Math.abs(squirrel[0] - nuts[i][0]) + Math.abs(squirrel[1] - nuts[i][1]); int diff = dist2tree[i] - dist2squir[i]; if (diff > maxDiff) maxDiff = diff; } return sum2tree * 2 - maxDiff; } }For n nuts, only n-1 of them would contribute 2 * distance_of_nut[i]_to_tree to the total distance. The other one, which is the first one picked up would contribute distance_of_nut[i]_to_tree + distance_of_nut[i]_to_squirrel to the total distance. To minimize the total distance, pick, as the first nut to pick up, the nut with the maximum 2 * distance_of_nut[i]_to_tree - (distance_of_nut[i]_to_tree + distance_of_nut[i]_to_squirrel).