题目如下:

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

解题思路:我的方法是把costs按照costs[i][0] - costs[i][1]的差值从小到大排序,然后前面一半的人去A,后面一半的人去B。至于为什么这样做,我也说不上来,感觉。

代码如下:

class Solution(object):
def twoCitySchedCost(self, costs):
"""
:type costs: List[List[int]]
:rtype: int
"""
def cmpf(item1,item2):
d1 = item1[0] - item1[1]
d2 = item2[0] - item2[1]
return d1 - d2 costs.sort(cmp=cmpf)
res = 0
for i in range(len(costs)):
if i < len(costs)/2:
res += costs[i][0]
else:
res += costs[i][1]
#print costs
return res

【leetcode】1029. Two City Scheduling的更多相关文章

  1. 【LeetCode】1029. Two City Scheduling 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 小根堆 排序 日期 题目地址:https://lee ...

  2. 【Leetcode_easy】1029. Two City Scheduling

    problem 1029. Two City Scheduling 参考 1. Leetcode_easy_1029. Two City Scheduling; 完

  3. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  4. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  5. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  6. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  7. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  8. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

  9. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

随机推荐

  1. Linux网卡驱动分析

    以太网(Ethernet)是一种计算机局域网组网技术,基于IEEE 802.3标准,它规定了包括物理层的连线.电信号和介质访问层协议. Ethernet接口的实质是MAC通过MII总线控制PHY的过程 ...

  2. 通过HookNtCreateSection 动态监控驱动sys、动态链接库dll、可执行文件exe加载

    [cpp] view plaincopyprint? /* windows2003 x86/x64 window7 x86 windows2008 R2 x64测试通过 */ #include < ...

  3. The import org.apache.hadoop.mapreduce cannot be resolved

    ubuntu@VM---ubuntu:~$ sudo apt--src.tar.gz Reading package lists... Done Building dependency tree Re ...

  4. mysql依据某一张表的字段,查询出对应的表所在的数据库

    表太多,只记得这个表有一个mygame的字段,但是并不知道这张表在那个数据库下,只能根据这个字段查找对应的表和所在数据库 select table_schema,table_name from inf ...

  5. python-笔记(一)python简介和入门

    一.什么是python? python是一种面向对象.解释型的计算机语言,它的特点是语法简洁.优雅.简单易学.在1989诞生,Guido(龟叔)开发.这里的python并不是蟒蛇的意思,而是龟叔非常喜 ...

  6. VMware 虚拟化编程(10) — VMware 数据块修改跟踪技术 CBT

    目录 目录 前文列表 数据块修改跟踪技术 CBT 为虚拟机开启 CBT CBT 修改数据块偏移量获取函数 QueryChangedDiskAreas changeId 一个 QueryChangedD ...

  7. Numpy 里线性代数函数

    c

  8. VS2013中使用本地IIS+域名调试ASP.NET项目

    VS2013中使用本地IIS+域名调试ASP.NET项目 在有些情况下需要使用本地的IIS作为调试服务器,如支持多域名的网站,这里记录下如何使用. 1.修改本机hosts文件. 路径:C:\Windo ...

  9. struts2 基础2 类型转换器

    struts2常用常量的定义与意义 每一次请求都会创建一个新的action,所以struts2的action是线程安全的 拆分struts 为应用指定多个struts配置文件 src 下为各应用配置的 ...

  10. scrapy爬取booking酒店评论数据

    # scrapy爬取酒店评论数据 -- 代码 here:github地址:https://github.com/760730895/scrapy_Booking--  采用scrapy爬取酒店评论数据 ...