Skip to content

Commit

Permalink
Create: 1029-two-city-scheduling.go / .py / .java
Browse files Browse the repository at this point in the history
  • Loading branch information
AkifhanIlgaz committed Jan 9, 2023
1 parent 2ae4b28 commit df542a5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
17 changes: 17 additions & 0 deletions go/1029-two-city-scheduling.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import "sort"

func main() {

}

func twoCitySchedCost(costs [][]int) int {
sort.Slice(costs, func(a, b int) bool { return costs[a][1] - costs[a][0] < costs[b][1] - costs[b][0]});

n ,totalCost := len(costs) / 2, 0
for i := 0; i < n ; i++ {
totalCost += costs[i][1] + costs[i +n][0]
}
return totalCost
}
15 changes: 15 additions & 0 deletions java/1029-two-city-scheduling.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public int twoCitySchedCost(int[][] costs) {
Arrays.sort(costs,
(a, b) -> (a[1] - a[0]) - (b[1] - b[0])
);

int n = costs.length / 2;
int total = 0;
for (int i = 0; i < n; i++) {
total += costs[i][1] + costs[i + n][0];
}

return total;
}
}
13 changes: 13 additions & 0 deletions python/1029-two-city-scheduling.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution:
def twoCitySchedCost(self, costs: List[List[int]]) -> int:
diffs = []
for c1, c2 in costs:
diffs.append([c2 - c1, c1, c2])
diffs.sort()
res = 0
for i in range(len(diffs)):
if i < len(diffs) / 2:
res += diffs[i][2]
else:
res += diffs[i][1]
return res

0 comments on commit df542a5

Please sign in to comment.