作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/two-city-scheduling/

题目描述

There are 2N people a company is planning to interview. The cost of flying the i-th person to city A is costs[i][0], and the cost of flying the i-th person to city B is costs[i][1].

Return the minimum cost to fly every person to a city such that exactly N people arrive in each city.

Example 1:

Input: [[10,20],[30,200],[400,50],[30,20]]
Output: 110
Explanation:
The first person goes to city A for a cost of 10.
The second person goes to city A for a cost of 30.
The third person goes to city B for a cost of 50.
The fourth person goes to city B for a cost of 20. The total minimum cost is 10 + 30 + 50 + 20 = 110 to have half the people interviewing in each city.

Note:

  1. 1 <= costs.length <= 100
  2. It is guaranteed that costs.length is even.
  3. 1 <= costs[i][0], costs[i][1] <= 1000

题目大意

给出了偶数个候选人去A和B两个城市的花费,现在要合理分配,让两个城市的人一样多,并且总花费最少。求最少花费。

解题方法

小根堆

思路怎么来的,是我划了一个表格:

编号
去A的花费 10 30 400 30
去B的花费 20 200 50 40
B-A +20 +170 -350 -10

根据表格我们可以想到,如果让丙去A,那么会比让丙去B多花350,这样多花费的钱划不来。所以,我们一定让去B比去A花费节省最多的人去B,反之,去A比去B花费节省最多的人去A。故这是一个贪心算法。

具体做法是我们求出每个人B-A的值,让去B能省下最省钱的一半人先去B,剩下的一半人去A.我们可以使用堆或者排序去完成这个事情。

class Solution(object):
def twoCitySchedCost(self, costs):
"""
:type costs: List[List[int]]
:rtype: int
"""
heap = []
for i, cost in enumerate(costs):
heapq.heappush(heap, (cost[1] - cost[0], i))
res = 0
count = 0
while heap:
cost, pos = heapq.heappop(heap)
if count < len(costs) / 2:
res += costs[pos][1]
else:
res += costs[pos][0]
count += 1
return res

排序

道理和上面类似。

class Solution(object):
def twoCitySchedCost(self, costs):
"""
:type costs: List[List[int]]
:rtype: int
"""
_len = len(costs)
cost_diff = []
for i, cost in enumerate(costs):
cost_diff.append((cost[1] - cost[0], i))
cost_diff.sort()
res = 0
count = 0
for i, (diff, pos) in enumerate(cost_diff):
if i < _len / 2:
res += costs[pos][1]
else:
res += costs[pos][0]
return res

日期

2019 年 8 月 31 日 —— 赶在月底做个题

【LeetCode】1029. Two City Scheduling 解题报告(Python)的更多相关文章

  1. 【LeetCode】62. Unique Paths 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...

  2. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  3. 【LeetCode】649. Dota2 Senate 解题报告(Python)

    [LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  4. 【LeetCode】911. Online Election 解题报告(Python)

    [LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  5. 【LeetCode】886. Possible Bipartition 解题报告(Python)

    [LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...

  6. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

  7. 【LeetCode】870. Advantage Shuffle 解题报告(Python)

    [LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...

  8. 【LeetCode】593. Valid Square 解题报告(Python)

    [LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  9. 【LeetCode】435. Non-overlapping Intervals 解题报告(Python)

    [LeetCode]435. Non-overlapping Intervals 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemi ...

随机推荐

  1. CentOS6.9安装python3

    安装依赖包: yum install -y openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel w ...

  2. Oracle-怎么在表的特定位置增加列

    create table tmp as select ID,UserName,RealName,Sex,Tel,Addr from tabName;drop table tabName;rename ...

  3. 2019java面试

    1.面向对象的特征有哪些方面?答:面向对象的特征主要有以下几个方面:    抽象:抽象是将一类对象的共同特征总结出来构造类的过程,包括数据抽象和行为抽象两方面.抽象只关注对象有哪些属性和行为,并不关注 ...

  4. POLLOUT/POLLINT事件触发测试

    一般poll使用过程中都是检测POLLIN事件,表示描述符有事件可以处理了,需要我们处理.对POLLOUT事件触发的方式相对较少,网上也有很多对此触发的疑问,利用实际项目中用的一个用法,下面做了个测试 ...

  5. day35前端基础之BOM和DOM

    day35前端基础之BOM和DOM BOM操作 简介 BOM(Browser Object Model)是指浏览器对象模型,它使 JavaScript 有能力与浏览器进行"对话". ...

  6. Netty之Channel*

    Netty之Channel* 本文内容主要参考**<<Netty In Action>> ** 和Netty的文档和源码,偏笔记向. 先简略了解一下ChannelPipelin ...

  7. 《C陷阱与缺陷》 第0章导读 第1章词法陷阱

    1.= 与==的区别 赋值运算符= 的优先级要小于逻辑运算符== 也就是说,会进行先逻辑上的比较,然后再把比较结果进行赋值,很合理. getc库是什么??? 1.C语言中有单字符 = 也有多字符单元如 ...

  8. 字节数与字符数mysql_mysql里一个中文汉字占多少字节数?

    在mysql中,如果是latin1字符集下,一个中文汉字占2个字节数:如果是utf8字符集下,一个中文汉字占3个字节数:如果是gbk字符集下,一个中文汉字占2个字节数. mysql各字符集下汉字和字母 ...

  9. 【Spring Framework】Spring入门教程(八)Spring的事务管理

    事务是什么? 事务:指单个逻辑操作单元的集合. 在操作数据库时(增删改),如果同时操作多次数据,我们从业务希望,要么全部成功,要么全部失败.这种情况称为事务处理. 例如:A转账给B. 第一步,扣除A君 ...

  10. webpack配置(vue)

    Vue-loader Vue-loader 是一个加载器,能把 .vue 文件转换为js模块. Vue Loader 的配置和其它的 loader 不太一样.除了将 vue-loader 应用到所有扩 ...