Skip to content

Commit

Permalink
Create: 1029-two-city-scheduling.rs / .ts / .js
Browse files Browse the repository at this point in the history
  • Loading branch information
AkifhanIlgaz committed Jan 9, 2023
1 parent ee7a3bc commit 2ae4b28
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
14 changes: 14 additions & 0 deletions javascript/1029-two-city-scheduling.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @param {number[][]} costs
* @return {number}
*/
const twoCitySchedCost = (costs) => {
costs.sort((a, b) => a[1] - a[0] - (b[1] - b[0]));

let totalCost = 0;
let n = costs.length / 2;
for (let i = 0; i < n; i++) {
totalCost += costs[i][1] + costs[i + n][0];
}
return totalCost;
};
15 changes: 15 additions & 0 deletions rust/1029-two-city-scheduling.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
impl Solution {
pub fn two_city_sched_cost(costs: Vec<Vec<i32>>) -> i32 {
let mut costs = costs;
costs.sort_by_key(|pair| pair[1] - pair[0]);

let mut total_cost = 0;

let n = costs.len() / 2;

for i in 0..n {
total_cost += costs[i][1] + costs[i + n][0];
}
total_cost
}
}
10 changes: 10 additions & 0 deletions typescript/1029-two-city-scheduling.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function twoCitySchedCost(costs: number[][]): number {
costs.sort((a, b) => a[1] - a[0] - (b[1] - b[0]));

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

0 comments on commit 2ae4b28

Please sign in to comment.