Skip to content

Commit 7fe312a

Browse files
committed
feat: add solutions to lc problem: No.1029
No.1029.Two City Scheduling
1 parent 7194bce commit 7fe312a

File tree

7 files changed

+105
-56
lines changed

7 files changed

+105
-56
lines changed

solution/1000-1099/1025.Divisor Game/README.md

+12-10
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,24 @@
5454

5555
**方法一:数学归纳法**
5656

57-
- $n=1$,先手败
58-
- $n=2$,先手拿 $1$,剩下 $1$,后手败,先手胜
59-
- $n=3$,先手拿 $1$,剩下 $2$,后手胜,先手败
60-
- $n=4$,先手拿 $1$,剩下 $3$,后手败,先手胜
57+
- $n=1$,先手败
58+
- $n=2$,先手拿 $1$,剩下 $1$,后手败,先手胜
59+
- $n=3$,先手拿 $1$,剩下 $2$,后手胜,先手败
60+
- $n=4$,先手拿 $1$,剩下 $3$,后手败,先手胜
6161
- ...
6262

63-
猜想,$n$ 为奇数时,先手败;$n$ 为偶数时,先手胜。
63+
猜想,$n$ 为奇数时,先手败;$n$ 为偶数时,先手胜。
6464

6565
证明:
6666

67-
1. $n=1$ 或 $n=2$,结论成立;
68-
1. $n>2$ 时,假设 $n \le k$ 时,该结论成立,则 $n=k+1$ 时:
69-
- 若 $k+1$ 为奇数,$x$ 是 $k+1$ 的因数,$x$ 只可能是奇数,那么 $k+1-x$ 为偶数,后手胜,先手败;
70-
- 若 $k+1$ 为偶数,$x$ 既可以是奇数 $1$,也可以是偶数,$x$ 取奇数,那么 $k+1-x$ 为奇数,后手败,先手胜。
67+
1. $n=1$ 或 $n=2$,结论成立;
68+
1. 若 $n \gt 2$,假设 $n \le k$ 时,该结论成立,则 $n=k+1$ 时:
69+
- 若 $k+1$ 为奇数,由于 $x$ 是 $k+1$ 的因数,那么 $x$ 只可能是奇数,因此 $k+1-x$ 为偶数,后手胜,先手败;
70+
- 若 $k+1$ 为偶数,此时 $x$ 既可以是奇数 $1$,也可以是偶数,$x$ 取奇数,那么 $k+1-x$ 为奇数,后手败,先手胜。
7171

72-
综上,$n$ 为奇数时,先手败;$n$ 为偶数时,先手胜。结论正确。
72+
综上,当 $n$ 为奇数时,先手败;当 $n$ 为偶数时,先手胜。结论正确。
73+
74+
时间复杂度 $O(1)$,空间复杂度 $O(1)$。
7375

7476
<!-- tabs:start -->
7577

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

+40-20
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,15 @@
5555

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

58-
**方法一:贪心**
58+
**方法一:排序 + 贪心**
5959

60-
选出 `aCost - bCost` 最小的 N 个人,让他们飞往 a 市,其余人飞往 b 市。
60+
我们不妨先假设所有人都去 $b$ 市,然后我们要从中选出 $n$ 个人去 $a$ 市,使得总费用最小。如果一个人去 $a$ 市的费用比去 $b$ 市的费用小,我们把这个人从 $b$ 市调到 $a$ 市,这样总费用就会减少。因此,我们可以将所有人按照去 $a$ 市的费用与去 $b$ 市的费用的差值从小到大排序,然后选出前 $n$ 个人去 $a$ 市,剩下的人去 $b$ 市,这样总费用就是最小的。
61+
62+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组 `costs` 的长度。
63+
64+
相似题目:
65+
66+
- [2611. 老鼠和奶酪](/solution/2600-2699/2611.Mice%20and%20Cheese/README.md)
6167

6268
<!-- tabs:start -->
6369

@@ -91,32 +97,17 @@ class Solution {
9197
}
9298
```
9399

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

111102
```cpp
112103
class Solution {
113104
public:
114105
int twoCitySchedCost(vector<vector<int>>& costs) {
115-
sort(costs.begin(), costs.end(), [](const std::vector<int>& a, const std::vector<int>& b) {
116-
return a[0] - a[1] < (b[0] - b[1]);
106+
sort(costs.begin(), costs.end(), [](const vector<int>& a, const vector<int>& b) {
107+
return a[0] - a[1] < b[0] - b[1];
117108
});
109+
int n = costs.size() / 2;
118110
int ans = 0;
119-
int n = costs.size() >> 1;
120111
for (int i = 0; i < n; ++i) {
121112
ans += costs[i][0] + costs[i + n][1];
122113
}
@@ -125,6 +116,35 @@ public:
125116
};
126117
```
127118
119+
### **Go**
120+
121+
```go
122+
func twoCitySchedCost(costs [][]int) (ans int) {
123+
sort.Slice(costs, func(i, j int) bool {
124+
return costs[i][0]-costs[i][1] < costs[j][0]-costs[j][1]
125+
})
126+
n := len(costs) >> 1
127+
for i, a := range costs[:n] {
128+
ans += a[0] + costs[i+n][1]
129+
}
130+
return
131+
}
132+
```
133+
134+
### **TypeScript**
135+
136+
```ts
137+
function twoCitySchedCost(costs: number[][]): number {
138+
costs.sort((a, b) => a[0] - a[1] - (b[0] - b[1]));
139+
const n = costs.length >> 1;
140+
let ans = 0;
141+
for (let i = 0; i < n; ++i) {
142+
ans += costs[i][0] + costs[i + n][1];
143+
}
144+
return ans;
145+
}
146+
```
147+
128148
### **...**
129149

130150
```

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

+32-18
Original file line numberDiff line numberDiff line change
@@ -79,32 +79,17 @@ class Solution {
7979
}
8080
```
8181

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

9984
```cpp
10085
class Solution {
10186
public:
10287
int twoCitySchedCost(vector<vector<int>>& costs) {
103-
sort(costs.begin(), costs.end(), [](const std::vector<int>& a, const std::vector<int>& b) {
104-
return a[0] - a[1] < (b[0] - b[1]);
88+
sort(costs.begin(), costs.end(), [](const vector<int>& a, const vector<int>& b) {
89+
return a[0] - a[1] < b[0] - b[1];
10590
});
91+
int n = costs.size() / 2;
10692
int ans = 0;
107-
int n = costs.size() >> 1;
10893
for (int i = 0; i < n; ++i) {
10994
ans += costs[i][0] + costs[i + n][1];
11095
}
@@ -113,6 +98,35 @@ public:
11398
};
11499
```
115100
101+
### **Go**
102+
103+
```go
104+
func twoCitySchedCost(costs [][]int) (ans int) {
105+
sort.Slice(costs, func(i, j int) bool {
106+
return costs[i][0]-costs[i][1] < costs[j][0]-costs[j][1]
107+
})
108+
n := len(costs) >> 1
109+
for i, a := range costs[:n] {
110+
ans += a[0] + costs[i+n][1]
111+
}
112+
return
113+
}
114+
```
115+
116+
### **TypeScript**
117+
118+
```ts
119+
function twoCitySchedCost(costs: number[][]): number {
120+
costs.sort((a, b) => a[0] - a[1] - (b[0] - b[1]));
121+
const n = costs.length >> 1;
122+
let ans = 0;
123+
for (let i = 0; i < n; ++i) {
124+
ans += costs[i][0] + costs[i + n][1];
125+
}
126+
return ans;
127+
}
128+
```
129+
116130
### **...**
117131

118132
```

solution/1000-1099/1029.Two City Scheduling/Solution.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
class Solution {
22
public:
33
int twoCitySchedCost(vector<vector<int>>& costs) {
4-
sort(costs.begin(), costs.end(), [](const std::vector<int>& a, const std::vector<int>& b) {
5-
return a[0] - a[1] < (b[0] - b[1]);
4+
sort(costs.begin(), costs.end(), [](const vector<int>& a, const vector<int>& b) {
5+
return a[0] - a[1] < b[0] - b[1];
66
});
7+
int n = costs.size() / 2;
78
int ans = 0;
8-
int n = costs.size() >> 1;
99
for (int i = 0; i < n; ++i) {
1010
ans += costs[i][0] + costs[i + n][1];
1111
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
func twoCitySchedCost(costs [][]int) int {
1+
func twoCitySchedCost(costs [][]int) (ans int) {
22
sort.Slice(costs, func(i, j int) bool {
33
return costs[i][0]-costs[i][1] < costs[j][0]-costs[j][1]
44
})
5-
ans, n := 0, len(costs)>>1
6-
for i := 0; i < n; i++ {
7-
ans += costs[i][0] + costs[i+n][1]
5+
n := len(costs) >> 1
6+
for i, a := range costs[:n] {
7+
ans += a[0] + costs[i+n][1]
88
}
9-
return ans
9+
return
1010
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function twoCitySchedCost(costs: number[][]): number {
2+
costs.sort((a, b) => a[0] - a[1] - (b[0] - b[1]));
3+
const n = costs.length >> 1;
4+
let ans = 0;
5+
for (let i = 0; i < n; ++i) {
6+
ans += costs[i][0] + costs[i + n][1];
7+
}
8+
return ans;
9+
}

solution/2600-2699/2611.Mice and Cheese/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@
6363

6464
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为奶酪的数量。
6565

66+
相似题目:
67+
68+
- [1029. 两地调度](/solution/1000-1099/1029.Two%20City%20Scheduling/README.md)
69+
6670
<!-- tabs:start -->
6771

6872
### **Python3**

0 commit comments

Comments
 (0)