LeetCode 1029. Two City Scheduling
考点 | 难度 |
---|---|
Greedy | Easy |
题目
A company is planning to interview 2n people. Given the array costs where costs[i] = [aCosti, bCosti], the cost of flying the ith person to city a is aCosti, and the cost of flying the ith person to city b is bCosti.
Return the minimum cost to fly every person to a city such that exactly n people arrive in each city.
思路
先假设都飞到第一个城市,然后用第二个城市-第一个城市计算refund list,sort list,选最小的。
答案
class Solution:
def twoCitySchedCost(self, costs):
refund = []
N = len(costs)//2
minCost = 0
for A, B in costs:
refund.append(B - A)
minCost += A
refund.sort()
for i in range(N):
minCost += refund[i]
return minCost