思路:
求出a-b的差值,并按照差值排序;
(这个差值相当于每个人浪费的钱,负数表示节约了钱。每个人浪费的钱最少则最后的花的钱最少)
最后取前n个去A地,后n个去B地。
class Solution:
def twoCitySchedCost(self, costs: List[List[int]]) -> int:
n = len(costs)
costs.sort(key = lambda x : x[0] - x[1])
return sum(list(map(lambda x:x[0],costs[:n//2]))+list(map(lambda x:x[1],costs[n//2:])))