forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create: 1029-two-city-scheduling.rs / .ts / .js
- Loading branch information
1 parent
ee7a3bc
commit 2ae4b28
Showing
3 changed files
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |