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. 解决VS2010使用mscomm控件无法接收数据的问题【转】

    之前有用过VC6的mscomm控件.所以这次也想继续用此控件实现此功能,结果没想到刚一上手还真的绕了不少弯子.主要是因为VC2010下对mscomm控件的添加,以及对控件成员变量的添加有点小繁琐,特此 ...

  2. getfacl语法2

    一.setfacl——设定文件访问控制列表语法: setfacl [-bkndRLP] { -m|-M|-x|-X ... } file ...  -m, --modify=acl 更改文件的访问控制 ...

  3. 入门项目 A1 start

    ''' 启动文件入口 ''' from core import src import os import sys # 拿到项目的路径 path = os.path.dirname(__file__) ...

  4. FreeSWITCH与FreeSWITCH对接

    (主机A ---> 主机B)192.168.100.A主机:修改/usr/local/freeswitch/conf/dialplan/default.xml 10         <ex ...

  5. 激活WIN10系统

    打开cmd slmgr /ipk VK7JG-NPHTM-C97JM-9MPGT-3V66T slmgr /skms kms.xspace.in slmgr /ato

  6. 《从Lucene到Elasticsearch:全文检索实战》学习笔记四

    今天我给大家讲讲布尔检索模型基本概念 布尔检索模型: 检索模型是判断文档内容与用户相关性的核心技术,以大规模网页搜索为例,在海量网页中与用户查询关键词相关的网页可能会有成千上万个,甚至耕读哦.那么信息 ...

  7. ViewpageAdapter

    import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory; ...

  8. Python - 统计一篇文章中单词的频率

    def frenquence_statistic(file_name): frequence = {} for line in open(file_name,'r').readlines(): wor ...

  9. switch留个爪,之后还需要再研究下

    public class SwitchDemo { public static void main (String [] args) { for(int i = 0; i < 10; i++) ...

  10. Struts2-052 RCE CVE-2017-9805

    从struts2的官网下载最后受影响的版本struts-2.5.12,地址: http://archive.apache.org/dist/struts/2.5.12/struts-2.5.12-ap ...