在Lyft投简历的时候给的一道题
java:
public class Test {
public static void main(String[] args) {
int[][]map = {{1,2,3,4},{5,0,7,8},{9,10,0,12},{13,14,15,0}};
calculateTwoDifferentWays(map);
}
public static void calculateTwoDifferentWays(int[][] map ){
// int[][] is a int[3][3] array, saving the distance between point A,B,C,D
//check that distance between every two points should be not negative number;
boolean isEveryDistanceNotNegative = true;
for (int i =0; i<4; i++)
for (int j=0; j<4; j++){
if (i==j) {if (map[i][j]!=0) isEveryDistanceNotNegative = false;}
else if (map[i][j] < 0 ) {isEveryDistanceNotNegative = false;}
}
if (isEveryDistanceNotNegative){
//from my understanding, driver one will start from point A and pick up driver two at point C, then come to point D, and end at point B
int driverOnePickUpDriverTwo = map[0][2]+map[2][3]+map[3][1];
//on the other hand, driver two will start from point C, pick up driver one at point A, then drop off driver one at point B, then back to poitn D
int driverTwoPickUpDriverOne = map[2][0]+map[0][1]+map[1][3];
//print the best choice and the minimum distance
int theMinimumDistance = Math.min(driverOnePickUpDriverTwo,driverTwoPickUpDriverOne);
if (driverOnePickUpDriverTwo == driverTwoPickUpDriverOne) {
System.out.print("Two different rides are the same distance, and the distance is: ");
}
else if (driverOnePickUpDriverTwo > driverTwoPickUpDriverOne){
System.out.print("Driver two pick up driver one will be a shorter distance, and the distance is: ");
}
else {
System.out.print("Driver one pick up driver two will be a shorter distance, and the distance is: ");
}
System.out.println(theMinimumDistance);
}
else System.out.println("Wrong data, has negative number between two points.");
}
}