美文网首页工作生活
1029. 两地调度

1029. 两地调度

作者: poteman | 来源:发表于2019-06-30 15:57 被阅读0次

对于一个人来说,他必定要去往A城市或者B城市,去往A城市的对比去往B城市的收益为两者之差,收益最大化就是最终结果的最小化。
计算一个gap矩阵为去往A城市和去往B城市费用之差,对gap矩阵排序获得idx,前N/2的人去往A城市,后N/2的人去往B城市。
细节: 数组排序获得index: idx = sorted(range(len(array)), key=lambda i:array[i])

class Solution(object):
    def twoCitySchedCost(self, costs):
        """
        :type costs: List[List[int]]
        :rtype: int
        """
        if not costs:
            return 0

        gap = []
        for i in range(len(costs)):
            gap.append(costs[i][0] - costs[i][1])
        idx = sorted(range(len(gap)), key=lambda i: gap[i])

        res = 0
        for i in idx[:len(idx)//2]:
            res += costs[i][0]
        for i in idx[len(idx)//2:]:
            res += costs[i][1]
        return res

相关文章

  • 1029. 两地调度

    对于一个人来说,他必定要去往A城市或者B城市,去往A城市的对比去往B城市的收益为两者之差,收益最大化就是最终结果的...

  • leetcode 1029. 两地调度

  • Leetcode 1029. 两地调度

    题目描述 公司计划面试 2N 人。第 i 人飞往 A 市的费用为 costs[i][0],飞往 B 市的费用为 c...

  • 1029. 两地调度(Python)

    难度:★★★☆☆类型:数组方法:贪心算法 题目 力扣链接请移步本题传送门[https://leetcode-cn....

  • 两地调度

    题目: 公司计划面试 2N 人。第 i 人飞往 A 市的费用为 costs[i][0],飞往 B 市的费用为 co...

  • 贪心--两地调度

    目录[https://www.jianshu.com/p/85e18c21317a] 题号[https://lee...

  • Day62 两地调度

    公司计划面试 2N 人。第 i 人飞往 A 市的费用为 costs[i][0],飞往 B 市的费用为 costs[...

  • 1029.旧键盘

    题目描述 旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现。现在给出应该输入的一段文字、以及实际被...

  • 高级调度(作业调度)、低级调度(进程调度)、中级调度

    处理机调度层次 调度层次分为三种 高级调度 = 作业调度 = 长程调度 低级调度 = 进程调度 = 短程调度 中级...

  • 关于kubernates的Pod调度策略

    Pod调度方式有,自动调度、定向调度、Node亲和性调度、Pod亲和性和互斥性调度 自动调度:Deployment...

网友评论

    本文标题:1029. 两地调度

    本文链接:https://www.haomeiwen.com/subject/qksbcctx.html