Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1978 from AkifhanIlgaz/1029
Browse files Browse the repository at this point in the history
Create: 1029-two-city-scheduling.rs / .ts / .js / .go / .py / .java
  • Loading branch information
tahsintunan authored Jan 10, 2023
2 parents bbc1e33 + df542a5 commit cc4c59e
Show file tree
Hide file tree
Showing 6 changed files with 84 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;
}
}
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;
};
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
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 cc4c59e

Please sign in to comment.