zoukankan      html  css  js  c++  java
  • 1029.两地调度

    公司计划面试 2N 人。第 i 人飞往 A 市的费用为 costs[i][0],飞往 B 市的费用为 costs[i][1]。
    返回将每个人都飞到某座城市的最低费用,要求每个城市都有 N 人抵达。
     
    示例:
    输入:[[10,20],[30,200],[400,50],[30,20]]
    输出:110
    解释:
    第一个人去 A 市,费用为 10。
    第二个人去 A 市,费用为 30。
    第三个人去 B 市,费用为 50。
    第四个人去 B 市,费用为 20。
    最低总费用为 10 + 30 + 50 + 20 = 110,每个城市都有一半的人在面试。

     
    提示:

     1 <= costs.length <= 100
     costs.length 为偶数
     1 <= costs[i][0], costs[i][1] <= 1000
     
    思路:先假设2N个人全去了B公司,然后再挑出N个人来让他们去A,然后每挑出一个人的改变的费用是 price_A - price_B (差值可正可负)所以想到,这个差值越小则总费用需要的就越小,因此我们就可以根据这个差值进行升序排列,让前N个人去A,让后N个人去B。
     1 int twoCitySchedCost(vector<vector<int>>& costs) 
     2 {
     3     sort(costs.begin(), costs.end(), [](vector<int> a, vector<int>b){return a[0] - a[1] < b[0] - b[1];});
     4     int n = costs.size() / 2, cost = 0;
     5     for(int i = 0; i < n; i++)
     6     {
     7         cost += (costs[i][0] + costs[i+n][1]);
     8     }
     9     return cost;
    10 }
     
  • 相关阅读:
    宏观经济指标
    线程与进程 concurrent.futures模块
    python 进程和线程(2)
    进程和线程(1)
    C++学习
    原则阅读笔记
    python类(3)感悟
    python类(2)
    打新股技巧
    python连接数据库
  • 原文地址:https://www.cnblogs.com/ZhengLijie/p/12826916.html
Copyright © 2011-2022 走看看