Skip to content

Commit cc4c59e

Browse files
authored
Merge pull request neetcode-gh#1978 from AkifhanIlgaz/1029
Create: 1029-two-city-scheduling.rs / .ts / .js / .go / .py / .java
2 parents bbc1e33 + df542a5 commit cc4c59e

File tree

6 files changed

+84
-0
lines changed

6 files changed

+84
-0
lines changed

go/1029-two-city-scheduling.go

Lines changed: 17 additions & 0 deletions
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+
}

java/1029-two-city-scheduling.java

Lines changed: 15 additions & 0 deletions
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+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @param {number[][]} costs
3+
* @return {number}
4+
*/
5+
const twoCitySchedCost = (costs) => {
6+
costs.sort((a, b) => a[1] - a[0] - (b[1] - b[0]));
7+
8+
let totalCost = 0;
9+
let n = costs.length / 2;
10+
for (let i = 0; i < n; i++) {
11+
totalCost += costs[i][1] + costs[i + n][0];
12+
}
13+
return totalCost;
14+
};

python/1029-two-city-scheduling.py

Lines changed: 13 additions & 0 deletions
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

rust/1029-two-city-scheduling.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
impl Solution {
2+
pub fn two_city_sched_cost(costs: Vec<Vec<i32>>) -> i32 {
3+
let mut costs = costs;
4+
costs.sort_by_key(|pair| pair[1] - pair[0]);
5+
6+
let mut total_cost = 0;
7+
8+
let n = costs.len() / 2;
9+
10+
for i in 0..n {
11+
total_cost += costs[i][1] + costs[i + n][0];
12+
}
13+
total_cost
14+
}
15+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function twoCitySchedCost(costs: number[][]): number {
2+
costs.sort((a, b) => a[1] - a[0] - (b[1] - b[0]));
3+
4+
let totalCost = 0;
5+
let n = costs.length / 2;
6+
for (let i = 0; i < n; i++) {
7+
totalCost += costs[i][1] + costs[i + n][0];
8+
}
9+
return totalCost;
10+
}

0 commit comments

Comments
 (0)