In time, you will call me master -- Star Wars
Week 1 Two City Scheduling
greedy
description :
Solution :
class Solution:
def twoCitySchedCost(self, costs: List[List[int]]) -> int:
diff_cost = list()
total_cost = 0
for i in range(0, len(costs)):
diff_cost.append(costs[i][1] - costs[i][0])
total_cost += costs[i][0]
diff_cost = sorted(diff_cost)
for i in range(0, len(costs)//2):
total_cost += diff_cost[i]
return total_cost
Solution detail :
中文版:
Person | City A cost | City B cost | move fee City B | sort
-------|-------------|-------------|-----------------------|
0 | 10 | 20 | 10 | -350
1 | 30 | 200 | 170 | -10
2 | 400 | 50 | -350 | 10
3 | 30 | 20 | -10 | 170
result : 10 + 30 + 400 + 30 -350 - 10 = 110