【LeetCode】1029. Two City Scheduling 解题报告(Python)
作者: 负雪明烛
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 <= costs.length <= 100
It is guaranteed that costs.length is even.
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)的更多相关文章
- 【LeetCode】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】649. Dota2 Senate 解题报告(Python)
[LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】911. Online Election 解题报告(Python)
[LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
- 【LeetCode】886. Possible Bipartition 解题报告(Python)
[LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
- 【LeetCode】870. Advantage Shuffle 解题报告(Python)
[LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...
- 【LeetCode】593. Valid Square 解题报告(Python)
[LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】435. Non-overlapping Intervals 解题报告(Python)
[LeetCode]435. Non-overlapping Intervals 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemi ...
随机推荐
- 关于GCC编译
GCC参数详解 gcc是gnu compiler collection 的简称,他包含了多种语言的编译器,如C, C++, Objective-C, Objective-C++, Java, Fort ...
- Oracle-like 多条件过滤以及and or用法
1.select * from file where DOC_SUBJECT not like '%测试%' and (DOC_STATUS like '待审' or DOC_STATUS li ...
- 基于tp5免费开源的后台管理系统
基于tp5免费开源的后台管理系统 可以自定义后台菜单,模块等. 后台模板用的是:AdminLTE 简单的后台基础管理系统,有兴趣开源看看 代码地址:https://github.com/mengzhi ...
- 省时省心DTM,广告转化无难题
内容来源:华为开发者大会2021 HMS Core 6 App Services技术论坛,主题演讲<华为分析服务,助您打造数智化运营闭环方案>. 演讲嘉宾:华为消费者云服务 分析产品总监 ...
- Java日期格式转换不用发愁
前言 Java 中日期.时间相关的类相当的多,并且分不同的版本提供了不同的实现,包括 Date . Calendar . LocalDateTime . ZoneDateTime . OffsetDa ...
- Vue中加载百度地图
借助百度地图的 LocalSearch 和 Autocomplete 两个方法 实现方式:通过promise以及百度地图的callback回调函数 map.js 1 export function M ...
- IPv6 私有地址
在互联网的地址架构中,专用网络是指遵守RFC 1918(IPV4)和RFC 4193(IPV6)规范,使用专用IP地址空间的网络.私有IP无法直接连接互联网,需要使用网络地址转换(Network Ad ...
- 自定义控件CustomAlertView
[记录][完整代码最下] 效果如下: 可行性分析: 由于系统自带的UIAlertView样式简单,只有两种样式,想要理想的样式就要自定义控件了 文件名取为:CustomAlertView 创建文件如下 ...
- MyBatis Collection小记—— 关联查询、递归查询、多字段关联
经常会用到mybatis的Collection标签来做级联查询或递归查询,现通过一个伪例来简单的说明一下使用中的关键点: 首先先列出三个表,给出一个场景: 1,角色表 t_role( id,name ...
- github单独下载某一个文件夹
可以借助svn工具进行下载,实现只下载repo下的指定文件夹内容 背景 需要下载这个文件夹下所有内容https://github.com/rabbitmq/rabbitmq-tutorials/tre ...