From d3da7e946874913cef803a47c7ae2f51d55a49f6 Mon Sep 17 00:00:00 2001 From: huangge1199 Date: Wed, 8 Sep 2021 16:26:07 +0800 Subject: [PATCH] =?UTF-8?q?1029:=E4=B8=A4=E5=9C=B0=E8=B0=83=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leetcode/editor/cn/TwoCityScheduling.java | 79 +++++++++++++++++++ .../cn/doc/content/TwoCityScheduling.md | 45 +++++++++++ 2 files changed, 124 insertions(+) create mode 100644 src/main/java/leetcode/editor/cn/TwoCityScheduling.java create mode 100644 src/main/java/leetcode/editor/cn/doc/content/TwoCityScheduling.md diff --git a/src/main/java/leetcode/editor/cn/TwoCityScheduling.java b/src/main/java/leetcode/editor/cn/TwoCityScheduling.java new file mode 100644 index 0000000..02f8c77 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/TwoCityScheduling.java @@ -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) + +} \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/doc/content/TwoCityScheduling.md b/src/main/java/leetcode/editor/cn/doc/content/TwoCityScheduling.md new file mode 100644 index 0000000..570b37c --- /dev/null +++ b/src/main/java/leetcode/editor/cn/doc/content/TwoCityScheduling.md @@ -0,0 +1,45 @@ +

公司计划面试 2n 人。给你一个数组 costs ,其中 costs[i] = [aCosti, bCosti] 。第 i 人飞往 a 市的费用为 aCosti ,飞往 b 市的费用为 bCosti

+ +

返回将每个人都飞到 ab 中某座城市的最低费用,要求每个城市都有 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,359],[631,42]]
+输出:3086
+
+ +

 

+ +

提示:

+ + +
Related Topics
  • 贪心
  • 数组
  • 排序

  • 👍 208
  • 👎 0
  • \ No newline at end of file