-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
40 lines (37 loc) · 1.14 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Solution {
public:
int twoCitySchedCost(vector<vector<int>>& costs) {
int n = size(costs) / 2;
vector<vector<int>> dp(n + 1,
vector<int>(n + 1, numeric_limits<int>::max()));
int sum_a = 0, sum_b = 0;
for (int i = 1; i <= n; ++i) {
sum_a += costs[i - 1][0];
sum_b += costs[i - 1][1];
dp[i][0] = sum_a;
dp[0][i] = sum_b;
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
dp[i][j] = min(dp[i - 1][j] + costs[i + j - 1][0],
dp[i][j - 1] + costs[i + j - 1][1]);
}
}
return dp[n][n];
}
};
class Solution {
public:
int twoCitySchedCost(vector<vector<int>>& costs) {
sort(begin(costs), end(costs),
[](const vector<int>& a, const vector<int>& b) {
return a[0] - a[1] < b[0] - b[1];
});
int n = size(costs) / 2;
int cost = 0;
for (int i = 0; i < n; ++i) {
cost += costs[i][0] + costs[i + n][1];
}
return cost;
}
};