Skip to content

Commit 1e55f6d

Browse files
authored
feat: add cpp solution to lc problem: No.1029 (doocs#772)
No.1029.Two City Scheduling
1 parent 707cc92 commit 1e55f6d

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,25 @@ func twoCitySchedCost(costs [][]int) int {
108108
}
109109
```
110110

111+
### **C++**
112+
113+
```cpp
114+
class Solution {
115+
public:
116+
int twoCitySchedCost(vector<vector<int>>& costs) {
117+
sort(costs.begin(), costs.end(), [](const std::vector<int> &a, const std::vector<int> &b) {
118+
return a[0] - a[1] < (b[0] - b[1]);
119+
});
120+
int ans = 0;
121+
int n = costs.size() >> 1;
122+
for (int i = 0; i < n; ++i) {
123+
ans += costs[i][0] + costs[i + n][1];
124+
}
125+
return ans;
126+
}
127+
};
128+
```
129+
111130
### **...**
112131
113132
```

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,25 @@ func twoCitySchedCost(costs [][]int) int {
9696
}
9797
```
9898

99+
### **C++**
100+
101+
```cpp
102+
class Solution {
103+
public:
104+
int twoCitySchedCost(vector<vector<int>>& costs) {
105+
sort(costs.begin(), costs.end(), [](const std::vector<int> &a, const std::vector<int> &b) {
106+
return a[0] - a[1] < (b[0] - b[1]);
107+
});
108+
int ans = 0;
109+
int n = costs.size() >> 1;
110+
for (int i = 0; i < n; ++i) {
111+
ans += costs[i][0] + costs[i + n][1];
112+
}
113+
return ans;
114+
}
115+
};
116+
```
117+
99118
### **...**
100119
101120
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
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]);
6+
});
7+
int ans = 0;
8+
int n = costs.size() >> 1;
9+
for (int i = 0; i < n; ++i) {
10+
ans += costs[i][0] + costs[i + n][1];
11+
}
12+
return ans;
13+
}
14+
};

0 commit comments

Comments
 (0)