Skip to content

Commit 2ae4b28

Browse files
committed
Create: 1029-two-city-scheduling.rs / .ts / .js
1 parent ee7a3bc commit 2ae4b28

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed
+14
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+
};

rust/1029-two-city-scheduling.rs

+15
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+
}
+10
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)