【leetcode】1029. Two City Scheduling
题目如下:
There are
2Npeople a company is planning to interview. The cost of flying thei-th person to cityAiscosts[i][0], and the cost of flying thei-th person to cityBiscosts[i][1].Return the minimum cost to fly every person to a city such that exactly
Npeople 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.lengthis even.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的更多相关文章
- 【LeetCode】1029. Two City Scheduling 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 小根堆 排序 日期 题目地址:https://lee ...
- 【Leetcode_easy】1029. Two City Scheduling
problem 1029. Two City Scheduling 参考 1. Leetcode_easy_1029. Two City Scheduling; 完
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
随机推荐
- a daemon 守护进程
w Cron and Crontab usage and exampleshttps://www.pantz.org/software/cron/croninfo.html
- 阶段1 语言基础+高级_1-3-Java语言高级_1-常用API_1_第6节 static静态_15_静态代码块
static的特殊用法, 静态代码块 加上构造方法,做测试 又创建一个对象 静态代码块 只执行一次 后续在学习jdbc的时候,静态代码块很有用途.
- debian下使用shell脚本时出现了 declare:not found 解决方法
问题:出现declare:not found的提示 解决:原来,UBUNTU用的是dash(后来证明这个其实这个不是错误的原因:从#!/bin/bash到#!/bin/dash,依旧无法运行,在这写出 ...
- python+selenium元素定位之CSS学习02
参考文档:https://www.runoob.com/cssref/css-selectors.html CSS选择器用于选择你想要的元素的样式的模式. "CSS"列表示在CSS ...
- java二周的学习总结
一转眼二周就过去了,个人觉得虽然java和C语言有差异,但差别并不大,因为语法语句方面都是差不多的,因为我上个学期并没有很认真的学好C语言,所以我这个学期更希望学好java,java方面还是挺有趣的, ...
- [BZOJ 2301] [HAOI 2011] Problem b (莫比乌斯反演)(有证明)
[BZOJ 2301] [HAOI 2011] Problem b (莫比乌斯反演)(有证明) 题面 T组询问,每次给出a,b,c,d,k,求\(\sum _{i=a}^b\sum _{j=c}^d[ ...
- vue 使用jssdk分享
背景 在vue中使用jssdk微信分享 weixin-js-sdk mint-ui需要安装npm install weixin-js-sdk mint-ui --save mixins/wechat. ...
- MVC框架与MTC框架
3.WEB框架 MVC Model View Controller 数据库 模板文件 业务处理 MTV Model Template View 数据库 模板文件 业务处理 ############## ...
- 多个nginx之间如何实现反向代理和负责均衡
1)nginx反向代理: http { upstream routeadmin { ip_hash; server 127.0.0.1:9201 weight= ...
- mysql考试复习
基础创建 字段自动编号auto_increment ( 单词补充:increment 定期的加薪; 增量; 增加) 考点 添加自增 alter table [表名] modify [字段(id)] i ...