Skip to content

Commit df542a5

Browse files
committed
Create: 1029-two-city-scheduling.go / .py / .java
1 parent 2ae4b28 commit df542a5

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

Diff for: go/1029-two-city-scheduling.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import "sort"
4+
5+
func main() {
6+
7+
}
8+
9+
func twoCitySchedCost(costs [][]int) int {
10+
sort.Slice(costs, func(a, b int) bool { return costs[a][1] - costs[a][0] < costs[b][1] - costs[b][0]});
11+
12+
n ,totalCost := len(costs) / 2, 0
13+
for i := 0; i < n ; i++ {
14+
totalCost += costs[i][1] + costs[i +n][0]
15+
}
16+
return totalCost
17+
}

Diff for: java/1029-two-city-scheduling.java

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int twoCitySchedCost(int[][] costs) {
3+
Arrays.sort(costs,
4+
(a, b) -> (a[1] - a[0]) - (b[1] - b[0])
5+
);
6+
7+
int n = costs.length / 2;
8+
int total = 0;
9+
for (int i = 0; i < n; i++) {
10+
total += costs[i][1] + costs[i + n][0];
11+
}
12+
13+
return total;
14+
}
15+
}

Diff for: python/1029-two-city-scheduling.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def twoCitySchedCost(self, costs: List[List[int]]) -> int:
3+
diffs = []
4+
for c1, c2 in costs:
5+
diffs.append([c2 - c1, c1, c2])
6+
diffs.sort()
7+
res = 0
8+
for i in range(len(diffs)):
9+
if i < len(diffs) / 2:
10+
res += diffs[i][2]
11+
else:
12+
res += diffs[i][1]
13+
return res

0 commit comments

Comments
 (0)