1029:两地调度

This commit is contained in:
huangge1199 2021-09-08 16:26:07 +08:00
parent 4036051760
commit d3da7e9468
2 changed files with 124 additions and 0 deletions

View File

@ -0,0 +1,79 @@
//公司计划面试 2n 给你一个数组 costs 其中 costs[i] = [aCosti, bCosti] i 人飞往 a 市的费用为
//aCosti 飞往 b 市的费用为 bCosti
//
// 返回将每个人都飞到 a b 中某座城市的最低费用要求每个城市都有 n 人抵达
//
//
//
// 示例 1
//
//
//输入costs = [[10,20],[30,200],[400,50],[30,20]]
//输出110
//解释
//第一个人去 a 费用为 10
//第二个人去 a 费用为 30
//第三个人去 b 费用为 50
//第四个人去 b 费用为 20
//
//最低总费用为 10 + 30 + 50 + 20 = 110每个城市都有一半的人在面试
//
//
// 示例 2
//
//
//输入costs = [[259,770],[448,54],[926,667],[184,139],[840,118],[577,469]]
//输出1859
//
//
// 示例 3
//
//
//输入costs = [[515,563],[451,713],[537,709],[343,819],[855,779],[457,60],[650,35
//9],[631,42]]
//输出3086
//
//
//
//
// 提示
//
//
// 2 * n == costs.length
// 2 <= costs.length <= 100
// costs.length 为偶数
// 1 <= aCosti, bCosti <= 1000
//
// Related Topics 贪心 数组 排序 👍 208 👎 0
package leetcode.editor.cn;
import java.util.Arrays;
//1029:两地调度
class TwoCityScheduling {
public static void main(String[] args) {
//测试代码
Solution solution = new TwoCityScheduling().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int twoCitySchedCost(int[][] costs) {
int[] a = new int[costs.length];
int sum = 0;
for (int i = 0; i < costs.length; i++) {
sum += costs[i][0];
a[i] = costs[i][1] - costs[i][0];
}
Arrays.sort(a);
for (int i = 0; i < a.length / 2; i++) {
sum+=a[i];
}
return sum;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,45 @@
<p>公司计划面试 <code>2n</code> 人。给你一个数组 <code>costs</code> ,其中 <code>costs[i] = [aCost<sub>i</sub>, bCost<sub>i</sub>]</code> 。第 <code>i</code> 人飞往 <code>a</code> 市的费用为 <code>aCost<sub>i</sub></code> ,飞往 <code>b</code> 市的费用为 <code>bCost<sub>i</sub></code></p>
<p>返回将每个人都飞到 <code>a</code><code>b</code> 中某座城市的最低费用,要求每个城市都有 <code>n</code> 人抵达<strong></strong></p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>costs = [[10,20],[30,200],[400,50],[30,20]]
<strong>输出:</strong>110
<strong>解释:</strong>
第一个人去 a 市,费用为 10。
第二个人去 a 市,费用为 30。
第三个人去 b 市,费用为 50。
第四个人去 b 市,费用为 20。
最低总费用为 10 + 30 + 50 + 20 = 110每个城市都有一半的人在面试。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>costs = [[259,770],[448,54],[926,667],[184,139],[840,118],[577,469]]
<strong>输出:</strong>1859
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>costs = [[515,563],[451,713],[537,709],[343,819],[855,779],[457,60],[650,359],[631,42]]
<strong>输出:</strong>3086
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>2 * n == costs.length</code></li>
<li><code>2 <= costs.length <= 100</code></li>
<li><code>costs.length</code> 为偶数</li>
<li><code>1 <= aCost<sub>i</sub>, bCost<sub>i</sub> <= 1000</code></li>
</ul>
<div><div>Related Topics</div><div><li>贪心</li><li>数组</li><li>排序</li></div></div><br><div><li>👍 208</li><li>👎 0</li></div>