题目如下:

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. Django中间件添加白名单

     一定记得配置 补充一点中间件是工作流程 中间件的详细流程 补充一点需求:在不用中间件的情况和下用装饰器做登陆的阻挡 在django中有自带的 登陆闭包函数只需要引出来就可以直接用了下面是步骤 在se ...

  2. 用Vue来实现音乐播放器(十七):歌手页右侧快速入口实现

    快速入口的列表是其实是之前处理的歌手的数据中的关于title的列表 shorcutList属性是计算属性   通过ret数组中的title计算到的 所以我们要在singer.vue组件中将数据传入到l ...

  3. MDX 入门

    之前用到的SQL,解释:结构化查询语言(Structured Query Language)(发音:/ˈes kjuː ˈel/ "S-Q-L"),是一种特殊目的的编程语言,是一种 ...

  4. floding regions

  5. 初窥AST

    一个简单的AST示例: AST结构: 里面有program.name.loc.type.comments.tokens 先看program: 重点关注program里面的body这个数组: JS引擎中 ...

  6. nginx的域名解析

    1.​创建域名解析结构: ngx_resolver_create(ngx_conf_t *cf, ngx_str_t *names, ngx_uint_t n) 这里面的names是dns服务器的地址 ...

  7. 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_07 缓冲流_2_BufferedOutputStream_字节缓冲

    子类 继承父类,这些方法都可以使用 必须写上flush,刷新数据数据才能写入到文件内

  8. postman+newman+jenkins接口自动化

    postman用来做接口测试非常方便,接口较多时,则可以实现接口自动化 目录 1.环境准备 2.本机调试脚本 3.集成jenkins 1.环境准备 1.1安装nodejs6.0+ 安装nodejs6. ...

  9. golang http Specifically check for timeout error

    Specifically check for timeout error 特异性识别 golang http client 的超时错误 package main import ( "fmt& ...

  10. C#实现京东登录密码加密POST

    1.京东登录登录密码 function getEntryptPwd(pwd) { var pubKey = $('#pubKey').val(); if (!pwd || !pubKey || !Sy ...