原题链接:Leecode 1029. 两地调度
假设所有人都去B地,对于所有人按找(去A的花费-去B的花费)的大小排序,排序后前N个人表示他们去A地比去B地更划算
class Solution {
public:
static bool cmp(const vector<int> &v1,const vector<int> &v2)
{
return v1[0]-v1[1]<v2[0]-v2[1];
}
int twoCitySchedCost(vector<vector<int>>& costs) {
int n=costs.size()/2;
sort(costs.begin(),costs.end(),cmp);
int res=0;
for(int i=0;i<n;i++)
res+=costs[i][0]+costs[i+n][1];
return res;
}
};