题目:
解题思路:
step1:计算出每个人去A地与去B地之差x;
step2:根据x对列表进行从小到大排序;
step3:取前一半的人去A地,取后一半的人去B地。
python源码:
from typing import List
class Solution:
def twoCitySchedCost(self, costs: List[List[int]]) -> int:
costs.sort(key=lambda x:(x[0]-x[1]))
length_costs=len(costs)
result=0
result+=sum(i[0] for i in costs[:length_costs//2])
result+=sum(i[1] for i in costs[length_costs//2:])
return result
costs=[[10,20],[30,200],[400,50],[30,20]]
so=Solution()
print(so.twoCitySchedCost(costs))