1
class Solution {
2
public:
3
int twoCitySchedCost(vector<vector<int>> &costs) {
4
sort(costs.begin(), costs.end(),
5
[](const vector<int> &curr,
6
const vector<int> &next) { // CUSTOM COMPARATOR
7
return (curr[0] - curr[1]) <
8
(next[0] - next[1]); // (comparing cost of sending to A - cost to B)
9
});
10
// original: [[10,20],[30,200],[400,50],[30,20]]
11
// after sort: [[30,200],[10,20],[30,20],[400,50]]
12
// to do: a a b b
13

14
int sum = 0;
15
for (int i = 0; i < costs.size() / 2; i++) {
16
sum += costs[i][0];
17
// cout<<costs[i][0]<<" "; // 30 10
18
}
19
for (int i = costs.size() / 2; i < costs.size(); i++) {
20
sum += costs[i][1];
21
// cout<<costs[i][1]<<" "; // 20 50
22
}
23

24
return sum;
25
}
26
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0