- 921. Minimum Add to Make Parentheses Valid 使括号有效的最少添加
- 915. Partition Array into Disjoint Intervals 分割数组
- 932. Beautiful Array 漂亮数组
- 940. Distinct Subsequences II 不同的子序列 II
题目地址: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、 Itisguaranteedthatcosts.lengthiseven.
;
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
道理和上面类似。
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
2022
DDKK.COM 弟弟快看-教程,程序员编程资料站,版权归原作者所有
本文经作者:负雪明烛 授权发布,任何组织或个人未经作者授权不得转发
我想将 [city]、[cIty]、[ciTY]、... 替换为 [City]。 我认为正则表达式是一个很好的解决方案,但我不擅长正则表达式。 尝试过的代码: var text = "[City] [
我正在使用 Ruby on Rails 制作一个网站,我正在考虑数据库结构。 这个网站很简单,我们列出了很多地方,并告诉他们来自哪个城市。 例如“埃菲尔铁塔”/巴黎或“帝国大厦”/纽约 对于数据库,哪
如果 Google 的关键字规划器向我显示关键字“Web Design [city-name]”获得约 880 次搜索,而“Website Design [city-name]”获得约 620 次搜索
这个脚本很好用,它生成函数的town必须注册但是他生成so city 如何将值 #city 插入/传递到输入字段 value="cityname" $(document).ready( funct
我有一个包含很多城市和两个名称相似的城市的数据库。 荷斯坦州奥尔登堡和奥尔登堡(奥尔登堡) 我从 URL 获取城市 示例网址: www.example.com/cityname/oldenburg-i
Cannot read property 'data' of undefined at Object.city 问题是我正在动态调用状态,当我编写city.UP.data时,它显示正确的结果,当我在此
我正在尝试将列表插入到 postgres 表中。 在 try 中,它在这一行失败了: curr.execute("""INSERT INTO CITY (name) values (%s);""",(
我想转换引用对象属性或子属性的 lambda 表达式,例如x => x.Address.City 转换成字符串 Address.City。是否存在执行此操作的现有框架方法(MVC、EF、...?),还
这些 linq 查询有什么区别: Students.Where(x=>x.City == "Lahore").ToList(); Students.ToList().Where(x=>x.City =
一天前我刚刚从 Xcode 9 更新到 Xcode 10,当我正要调试我的位置跟踪应用程序时才意识到我从未收到设备位置信息。所以我去 map 应用程序检查它是否只是我的应用程序的问题,但是当我将位置调
我安装了 django-cities 插件并在 settings.py 中设置了以下参数: CITIES_FILES = { 'city': { 'filena
题目地址:https://leetcode-cn.com/problems/destination-city/ 题目描述 给你一份旅游线路图,该线路图中的旅行线路用数组 paths 表示,其中 p
我已经配置了Team City通过EC2发送电子邮件,并且工作正常,但是现在如何设置电子邮件帐户以发送服务器上所有构建作业的失败通知?我敢打赌,答案是盯着我,但我只是看不见。 TC 7.x BTW。
将 TeamCity 构建代理安装在构建服务器之外的另一台机器上意味着什么?我的理解是,我需要“TeamCity 服务器安装”才能开始“代理安装”,对吗?现在我已经安装了带有 webUI 的 Team
我正在尝试在构建服务器(团队城市 6.5.5)上添加一个新的构建代理 - 我能够添加一个新的代理,但现在第一个已断开连接,第二个正在运行,我认为存在一些问题我指定的 ownport 和 ServerU
我在TeamCity项目中遇到以下错误: Error collecting changes for VCS repository 'MySvnRepository' Unable to get SVN
各位, 我正在尝试使用团队城市。我完成了 7 个步骤中的 6 个步骤。现在是最后一步,在“Agent Requirements”选项卡下,它向我显示以下消息 Agents compatibility
我们有 3 个环境: 开发:Team City 在此部署,以在主干上进行 Subversion 提交。 暂存:用户接受是在此处针对候选版本的构建完成的。 生产:当UAT通过时,通过的代码集部署在此处。
Gradle支持的最新TeamCity 7版本是什么? 最佳答案 通过使用gradle包装器,您应该能够在Teamcity 7中使用最新的2.8 gradle版本 关于gradle - Team Ci
生成项目时,通常的过程是,一旦将所有文件从源代码控制中拖放下来,就会运行AssemblyInfo修补程序。 使用CodeSmith时,我们对此有疑问。问题在于我们有两个构建步骤。 步骤1-运行Code
我是一名优秀的程序员,十分优秀!