Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcc4c59e

Browse files
authored
Merge pull requestneetcode-gh#1978 from AkifhanIlgaz/1029
Create: 1029-two-city-scheduling.rs / .ts / .js / .go / .py / .java
2 parentsbbc1e33 +df542a5 commitcc4c59e

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+
funcmain() {
6+
7+
}
8+
9+
functwoCitySchedCost(costs [][]int)int {
10+
sort.Slice(costs,func(a,bint)bool {returncosts[a][1]-costs[a][0]<costs[b][1]-costs[b][0]});
11+
12+
n ,totalCost:=len(costs)/2,0
13+
fori:=0;i<n ;i++ {
14+
totalCost+=costs[i][1]+costs[i+n][0]
15+
}
16+
returntotalCost
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+
classSolution {
2+
publicinttwoCitySchedCost(int[][]costs) {
3+
Arrays.sort(costs,
4+
(a,b) -> (a[1] -a[0]) - (b[1] -b[0])
5+
);
6+
7+
intn =costs.length /2;
8+
inttotal =0;
9+
for (inti =0;i <n;i++) {
10+
total +=costs[i][1] +costs[i +n][0];
11+
}
12+
13+
returntotal;
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+
consttwoCitySchedCost=(costs)=>{
6+
costs.sort((a,b)=>a[1]-a[0]-(b[1]-b[0]));
7+
8+
lettotalCost=0;
9+
letn=costs.length/2;
10+
for(leti=0;i<n;i++){
11+
totalCost+=costs[i][1]+costs[i+n][0];
12+
}
13+
returntotalCost;
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+
classSolution:
2+
deftwoCitySchedCost(self,costs:List[List[int]])->int:
3+
diffs= []
4+
forc1,c2incosts:
5+
diffs.append([c2-c1,c1,c2])
6+
diffs.sort()
7+
res=0
8+
foriinrange(len(diffs)):
9+
ifi<len(diffs)/2:
10+
res+=diffs[i][2]
11+
else:
12+
res+=diffs[i][1]
13+
returnres

‎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+
implSolution{
2+
pubfntwo_city_sched_cost(costs:Vec<Vec<i32>>) ->i32{
3+
letmut costs = costs;
4+
costs.sort_by_key(|pair| pair[1] - pair[0]);
5+
6+
letmut total_cost =0;
7+
8+
let n = costs.len() /2;
9+
10+
for iin0..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+
functiontwoCitySchedCost(costs:number[][]):number{
2+
costs.sort((a,b)=>a[1]-a[0]-(b[1]-b[0]));
3+
4+
lettotalCost=0;
5+
letn=costs.length/2;
6+
for(leti=0;i<n;i++){
7+
totalCost+=costs[i][1]+costs[i+n][0];
8+
}
9+
returntotalCost;
10+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp