使用贪心
首先按照两地之间距离最小进行排序,然后前一步去A点,后一半去b点
class Solution {
public int twoCitySchedCost(int[][] costs) {
int n = costs.length;
int res = 0;
Arrays.sort(costs, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o1[0] - o1[1] - (o2[0] - o2[1]);
}
});
for (int i = 0; i < n / 2; i++){
res += costs[i][0];
}
for (int j = n / 2; j < n ; j++){
res += costs[j][1];
}
return res;
}
}