Skip to content

Commit 2f93880

Browse files
committed
feat: add solutions to lc problem: No.1029
No.1029.Two City Scheduling
1 parent 262b700 commit 2f93880

File tree

5 files changed

+98
-19
lines changed

5 files changed

+98
-19
lines changed

Diff for: solution/1000-1099/1029.Two City Scheduling/README.md

+36-1
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,57 @@
5555

5656
<!-- 这里可写通用的实现逻辑 -->
5757

58+
**方法一:贪心**
59+
60+
选出 `aCost - bCost` 最小的 N 个人,让他们飞往 A 市,其余人飞往 B 市。
61+
5862
<!-- tabs:start -->
5963

6064
### **Python3**
6165

6266
<!-- 这里可写当前语言的特殊实现逻辑 -->
6367

6468
```python
65-
69+
class Solution:
70+
def twoCitySchedCost(self, costs: List[List[int]]) -> int:
71+
costs.sort(key=lambda x: x[0] - x[1])
72+
n = len(costs) >> 1
73+
return sum(costs[i][0] + costs[i + n][1] for i in range(n))
6674
```
6775

6876
### **Java**
6977

7078
<!-- 这里可写当前语言的特殊实现逻辑 -->
7179

7280
```java
81+
class Solution {
82+
public int twoCitySchedCost(int[][] costs) {
83+
Arrays.sort(costs, (a, b) -> {
84+
return a[0] - a[1] - (b[0] - b[1]);
85+
});
86+
int ans = 0;
87+
int n = costs.length >> 1;
88+
for (int i = 0; i < n; ++i) {
89+
ans += costs[i][0] + costs[i + n][1];
90+
}
91+
return ans;
92+
}
93+
}
94+
```
7395

96+
### **Go**
97+
98+
```go
99+
func twoCitySchedCost(costs [][]int) int {
100+
sort.Slice(costs, func(i, j int) bool {
101+
return costs[i][0]-costs[i][1] < costs[j][0]-costs[j][1]
102+
})
103+
ans, n := 0, len(costs)>>1
104+
for i := 0; i < n; i++ {
105+
ans += costs[i][0] + costs[i+n][1]
106+
}
107+
return ans
108+
}
74109
```
75110

76111
### **...**

Diff for: solution/1000-1099/1029.Two City Scheduling/README_EN.md

+34-1
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,51 @@ The total minimum cost is 10 + 30 + 50 + 20 = 110 to have half the people interv
4949

5050
## Solutions
5151

52+
Greedy.
53+
5254
<!-- tabs:start -->
5355

5456
### **Python3**
5557

5658
```python
57-
59+
class Solution:
60+
def twoCitySchedCost(self, costs: List[List[int]]) -> int:
61+
costs.sort(key=lambda x: x[0] - x[1])
62+
n = len(costs) >> 1
63+
return sum(costs[i][0] + costs[i + n][1] for i in range(n))
5864
```
5965

6066
### **Java**
6167

6268
```java
69+
class Solution {
70+
public int twoCitySchedCost(int[][] costs) {
71+
Arrays.sort(costs, (a, b) -> {
72+
return a[0] - a[1] - (b[0] - b[1]);
73+
});
74+
int ans = 0;
75+
int n = costs.length >> 1;
76+
for (int i = 0; i < n; ++i) {
77+
ans += costs[i][0] + costs[i + n][1];
78+
}
79+
return ans;
80+
}
81+
}
82+
```
6383

84+
### **Go**
85+
86+
```go
87+
func twoCitySchedCost(costs [][]int) int {
88+
sort.Slice(costs, func(i, j int) bool {
89+
return costs[i][0]-costs[i][1] < costs[j][0]-costs[j][1]
90+
})
91+
ans, n := 0, len(costs)>>1
92+
for i := 0; i < n; i++ {
93+
ans += costs[i][0] + costs[i+n][1]
94+
}
95+
return ans
96+
}
6497
```
6598

6699
### **...**
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
func twoCitySchedCost(costs [][]int) int {
2+
sort.Slice(costs, func(i, j int) bool {
3+
return costs[i][0]-costs[i][1] < costs[j][0]-costs[j][1]
4+
})
5+
ans, n := 0, len(costs)>>1
6+
for i := 0; i < n; i++ {
7+
ans += costs[i][0] + costs[i+n][1]
8+
}
9+
return ans
10+
}
+13-17
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
class Solution {
2-
public int twoCitySchedCost(int[][] costs) {
3-
Arrays.sort(costs, (a, b) -> {
4-
return a[0] - a[1] - (b[0] - b[1]);
5-
});
6-
7-
int sum = 0;
8-
for (int i = 0; i < costs.length; ++i) {
9-
if (i < costs.length / 2) {
10-
sum += costs[i][0];
11-
} else {
12-
sum += costs[i][1];
13-
}
14-
}
15-
return sum;
16-
}
17-
}
1+
class Solution {
2+
public int twoCitySchedCost(int[][] costs) {
3+
Arrays.sort(costs, (a, b) -> {
4+
return a[0] - a[1] - (b[0] - b[1]);
5+
});
6+
int ans = 0;
7+
int n = costs.length >> 1;
8+
for (int i = 0; i < n; ++i) {
9+
ans += costs[i][0] + costs[i + n][1];
10+
}
11+
return ans;
12+
}
13+
}
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution:
2+
def twoCitySchedCost(self, costs: List[List[int]]) -> int:
3+
costs.sort(key=lambda x: x[0] - x[1])
4+
n = len(costs) >> 1
5+
return sum(costs[i][0] + costs[i + n][1] for i in range(n))

0 commit comments

Comments
 (0)