leetcode1029
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的更多相关文章
- [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 ...
- LeetCode1029 两地调度(贪心+java自定义排序回顾)
题目: 公司计划面试 2N 人.第 i 人飞往 A 市的费用为 costs[i][0],飞往 B 市的费用为 costs[i][1]. 返回将每个人都飞到某座城市的最低费用,要求每个城市都有 N 人抵 ...
随机推荐
- 马凯军201771010116《面向对象程序设计(java)》第二周学习总结
第一部分:理论知识学习部分 (1)基本知识:简单应用程序的结构:Java环境里的注释方式: (2)数据类型(4种整型.2种浮点型.1种字符型‘char’.真值型‘Boolean’. (3)变量,每个变 ...
- idea取消参数名称(形参名)提示
idea取消参数名称(形参名)提示 IDEA会自动显示形式参数的变量名称,这在一开始使用时感觉很方便,友好.有时候也会显得排版很乱,下面是取消自动显示形式参数名称的方式 取消前是这个样子. “File ...
- React-Native新列表组件FlatList和SectionList学习 | | 联动列表实现
React-Native在0.43推出了两款新的列表组件:FlatList(高性能的简单列表组件)和SectionList(高性能的分组列表组件). 从官方上它们都支持常用的以下功能: 完全跨平台. ...
- Java错误:结束的字符文字
编译器为NetBeans 在学习java的时候突然出现了以下错误 错误代码是: Gen <Integer ,String> a = new Gen <Integer, String& ...
- django中向用户发送邮件信息
发送邮件来让用户激活,因此,邮件中需要包含用户信息.但用户信息需要加密才可以.因此加密采用的是itsdangerous中的TimedJSONWebSignatureSerializer. 参考链接:h ...
- [LeetCode&Python] Problem 118. Pascal's Triangle
Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's t ...
- jwt 接口加密
什么是JWT Json web token (JWT), 是为了在网络应用环境间传递声明而执行的一种基于JSON的开放标准((RFC 7519).该token被设计为紧凑且安全的,特别适用于分布式站点 ...
- I think I need a boat house
I think I need a boat house. Fred Mapper is considering purchasing some land in Louisiana to build h ...
- spring jpa + mybatis快速开始:
springmvc开始搭建 源码地址 https://gitee.com/flydb/spingjpamy pom: <packaging>war</packaging> &l ...
- s2第六章继承和多态
public class Employee { //年龄 public int Age { get; set; } //性别 public Gender Gender { get; set; } // ...