題目:公司計劃面試 2N 人。第 i 人飛往 A 市的費用爲 costs[i][0],飛往 B 市的費用爲 costs[i][1]。
返回將每個人都飛到某座城市的最低費用,要求每個城市都有 N 人抵達。
示例:
輸入:[[10,20],[30,200],[400,50],[30,20]]
輸出:110
解釋:
第一個人去 A 市,費用爲 10。
第二個人去 A 市,費用爲 30。
第三個人去 B 市,費用爲 50。
第四個人去 B 市,費用爲 20。
最低總費用爲 10 + 30 + 50 + 20 = 110,每個城市都有一半的人在面試。
來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/two-city-scheduling
著作權歸領釦網絡所有。商業轉載請聯繫官方授權,非商業轉載請註明出處。
代碼:
int twoCitySchedCost(int** costs, int costsSize, int* costsColSize){
int temp[costsSize],sum=0;
for(int i=0;i<costsSize;i++){
sum+=costs[i][0];
}
for(int i=0;i<costsSize;i++){
temp[i]=costs[i][0]-costs[i][1];
}
for(int i=0;i<costsSize-1;i++){
for(int j=0;j<costsSize-i-1;j++){
if(temp[j]>temp[j+1]){
int tem=temp[j];
temp[j]=temp[j+1];
temp[j+1]=tem;
}
}
}
for(int i=costsSize/2;i<costsSize;i++){
sum-=temp[i];
}
return sum;
}