public class Solution {
private int getDistance(int[] pos1, int[] pos2) {
return Math.abs(pos1[0] - pos2[0]) + Math.abs(pos1[1] - pos2[1]);
}
public int minDistance(int height, int width, int[] tree, int[] squirrel, int[][] nuts) {
int sumNuts2Tree = 0;
int numOfNuts = nuts.length;
int[] Nuts2Tree = new int[numOfNuts];
int minNuts2Squirrel = Integer.MAX_VALUE;
for (int i = 0; i < numOfNuts; i++) {
int d = getDistance(nuts[i], tree);
sumNuts2Tree += d * 2;
Nuts2Tree[i] = d;
}
for (int i = 0; i < numOfNuts; i++) {
minNuts2Squirrel = Math.min(minNuts2Squirrel, getDistance(nuts[i], squirrel) - Nuts2Tree[i]);
}
return sumNuts2Tree + minNuts2Squirrel;
}
}
public class Solution {
public int minDistance(int height, int width, int[] tree, int[] squirrel, int[][] nuts) {
int diff = Integer.MAX_VALUE;
int dist = 0;
for (int i = 0; i < nuts.length; i++) {
int val = distance(nuts[i], squirrel);
int val1 = distance(nuts[i], tree);
if (val - val1 <= diff)
diff = val-val1;
dist += 2 * val1;
}
dist += diff;
return dist;
}
private int distance(int[] nut, int[] squirrel) {
return Math.abs(nut[0] - squirrel[0]) + Math.abs(nut[1] - squirrel[1]);
}
}