LeetCode题解(1029):两地最优调度(Python)
发布日期:2021-06-29 19:55:15
浏览次数:4
分类:技术文章
本文共 831 字,大约阅读时间需要 2 分钟。
题目:(简单)
解法 | 时间复杂度 | 空间复杂度 | 执行用时 |
---|---|---|---|
Ans 1 (Python) | O ( N l o g N ) O(NlogN) O(NlogN) | O ( N ) O(N) O(N) | 48ms (87.28%) |
Ans 2 (Python) | O ( N l o g N ) O(NlogN) O(NlogN) | O ( N ) O(N) O(N) | 44ms (97.01%) |
Ans 3 (Python) |
LeetCode的Python执行用时随缘,只要时间复杂度没有明显差异,执行用时一般都在同一个量级,仅作参考意义。
解法一(自定义排序):
def twoCitySchedCost(self, costs: List[List[int]]) -> int: def differ(cost): return cost[0] - cost[1] costs.sort(key=differ) center = len(costs) // 2 ans = 0 for i in range(center): ans += costs[i][0] for i in range(center, len(costs)): ans += costs[i][1] return ans
解法二(优雅化):
def twoCitySchedCost(self, costs: List[List[int]]) -> int: costs.sort(key=lambda cost: cost[0] - cost[1]) center = len(costs) // 2 ans = 0 for i in range(center): ans += costs[i][0] + costs[i + center][1] return ans
转载地址:https://dataartist.blog.csdn.net/article/details/107100908 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
初次前来,多多关照!
[***.217.46.12]2024年04月11日 22时52分12秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
使用mysql自带工具mysqldump进行全库备份以及source命令恢复数据库
2019-04-30
决策树笔记(入门)
2019-04-30
Taro中如何使用cssModules
2019-04-30
taro中别名引入路径
2019-04-30
taro中hook的使用
2019-04-30
使用element-ui的table组件,使用三元表达式筛选数据
2019-04-30
vue打包项目报错Failed to load resource: the server responded with a status of 500 (Internal Server Error)
2019-04-30
解决小程序在高层级页面滑动时,低层级页面会跟着滑动的问题
2019-04-30
子组件watch监听父组件数据,从而判断是否重新加载数据
2019-04-30
小程序码生成和解析问题
2019-04-30
小程序修改回答功能实现
2019-04-30
vue中 指令与函数的结合使用
2019-04-30
微信小程序更新授权接口wx.getUserProfile后,个别用户出现无法登录的问题
2019-04-30
微信小程序canvas绘制圆角矩形
2019-04-30
微信小程序canvas裁剪图片中间部分
2019-04-30
my docker tutorial
2019-04-30
gdb debug summary
2019-04-30
python learning
2019-04-30
collections
2019-04-30
shallow copy & deep copy
2019-04-30