class Solution(object):
def twoCitySchedCost(self, costs: 'List[List[int]]') -> int:
costs = sorted(costs,key = lambda x: abs(x[0]-x[1]),reverse=True)
n = len(costs)
allcosts = 0
left = 0
right = 0
for i in range(n):
if costs[i][0] <= costs[i][1] and left < n//2:
left += 1
allcosts += costs[i][0]
else:
if right < n//2:
right += 1
allcosts += costs[i][1]
else:
left += 1
allcosts += costs[i][0]
return allcosts

贪心思想:根据人距离A,B城市的费用的“差值”,从大到小排序。排的靠前的优先选择其费用低的城市。如果某个城市的人数已经达到1/2,则剩下的全部选择另外一个城市。这种方式的总体的费用最低。

leetcode1029的更多相关文章

  1. [Swift]LeetCode1029. 两地调度 | Two City Scheduling

    There are 2N people a company is planning to interview. The cost of flying the i-th person to city A ...

  2. LeetCode1029 两地调度(贪心+java自定义排序回顾)

    题目: 公司计划面试 2N 人.第 i 人飞往 A 市的费用为 costs[i][0],飞往 B 市的费用为 costs[i][1]. 返回将每个人都飞到某座城市的最低费用,要求每个城市都有 N 人抵 ...

随机推荐

  1. Codeforces Round #324 (Div. 2) (哥德巴赫猜想)

    题目:http://codeforces.com/problemset/problem/584/D 思路: 关于偶数的哥德巴赫猜想:任一大于2的偶数都可写成两个素数之和. 关于奇数的哥德巴赫猜想:任一 ...

  2. 一款非常不错的重写listctrl类-CListCtrlEx

    原文在:https://www.codeproject.com/Articles/28063/An-Extended-MFC-CListCtrl-to-edit-individual-cells li ...

  3. 3.3 unittest批量执行

    3.3 unittest批量执行 我们在写用例的时候,单个脚本的用例好执行,那么多个脚本的时候,如何批量执行呢?这时候就需要用到unittet里面的discover方法来加载用例了.加载用例后,用un ...

  4. 初学java集合框架笔记

    List接口常用方法: 方法名 说    明 boolean add(Object o) 在列表的末尾顺序添加元素, 起始索引位置从0开始 void add(int index,Object o) 在 ...

  5. s21day07 python笔记

    s21day07 python笔记 一.昨日内容回顾及补充 回顾 补充 将前面所提到的功能,统一改称为方法 二.深浅拷贝 基本格式 v1 = [1,2,3] import copy v2 = copy ...

  6. Python全栈之路----函数进阶----作用域的查找空间

    n = 10 def func(): n = 20 print('func:',n) def func2(): n = 30 print('func2:',n) def func3(): print( ...

  7. 13.python错误和异常

    一.错误和异常1.程序中的错误分为俩种:(1)语法错误:不按照语言的规则,必须在程序执行前就改正(2)逻辑错误2.异常就是程序运行时发生错误的信号,分为三部分(1)Traceback:异常追踪的信息( ...

  8. nginx+keepalived实现高可用

    参看文献 https://blog.csdn.net/u012410733/article/details/57078407 nginx的安装,这里就不再讲了 这里使用了两台服务器 192.168.3 ...

  9. CodeForces - 13E

    Little Petya likes to play a lot. Most of all he likes to play a game «Holes». This is a game for on ...

  10. MySQL数据库-pymysql模块操作数据库

    pymysql模块是python操作数据库的一个模块 connect()创建数据库链接,参数是连接数据库需要的连接参数使用方式: 模块名称.connect() 参数: host=数据库ip port= ...