Formatted question description: https://leetcode.ca/all/1029.html

# 1029. Two City Scheduling (Easy)

There are 2N people a company is planning to interview. The cost of flying the i-th person to city A is costs[i], and the cost of flying the i-th person to city B is costs[i].

Return the minimum cost to fly every person to a city such that exactly N people arrive in each city.

Example 1:

Input: [[10,20],[30,200],[400,50],[30,20]]
Output: 110
Explanation:
The first person goes to city A for a cost of 10.
The second person goes to city A for a cost of 30.
The third person goes to city B for a cost of 50.
The fourth person goes to city B for a cost of 20.

The total minimum cost is 10 + 30 + 50 + 20 = 110 to have half the people interviewing in each city.


Note:

1. 1 <= costs.length <= 100
2. It is guaranteed that costs.length is even.
3. 1 <= costs[i], costs[i] <= 1000

Related Topics:
Greedy

## Solution 1. DP

Let dp[i + 1][j] be the min cost arranging the first i + 1 people and when j people go to city A, 0 <= i < N, 0 <= j <= i + 1.

For dp[i + 1][j], we have two options:

• The i-th person goes to city A. We get dp[i][j - 1] + A[i].
• The i-th person goes to city B. We get dp[i][j] + A[i]. Note that i + 1 > j because otherwise we don’t have the spot for the i-th person to go to city B.
dp[i + 1][j] = min(
dp[i][j - 1] + A[i],                     // the i-th person goes to city A
i + 1> j  ? dp[i][j] + A[i] : INF        // the i-th person goes to city B
)
dp[i + 1] = sum( A[k] | 0 <= k <= i )

// OJ: https://leetcode.com/problems/two-city-scheduling/

// Time: O(N^2)
// Space: O(N^2)
class Solution {
public:
int twoCitySchedCost(vector<vector<int>>& A) {
int N = A.size();
vector<vector<int>> dp(N + 1, vector<int>(N / 2 + 1));
for (int i = 0; i < N; ++i) {
dp[i + 1] = dp[i] + A[i];
for (int j = 1; j <= min(i + 1, N / 2); ++j) {
dp[i + 1][j] = min((i + 1 > j ? dp[i][j] + A[i] : INT_MAX), dp[i][j - 1] + A[i]);
}
}
return dp[N][N / 2];
}
};


## Solution 2. DP with Space Optimization

// OJ: https://leetcode.com/problems/two-city-scheduling/

// Time: O(N^2)
// Space: O(N)
class Solution {
public:
int twoCitySchedCost(vector<vector<int>>& A) {
int N = A.size();
vector<int> dp(N / 2 + 1);
for (int i = 0; i < N; ++i) {
for (int j = min(i + 1, N / 2); j >= 1; --j) {
dp[j] = min((i + 1 > j ? dp[j] + A[i] : INT_MAX), dp[j - 1] + A[i]);
}
dp += A[i];
}
return dp[N / 2];
}
};


## Solution 3. Greedy

The smaller cost[i] - cost[i] is, the more likely i-th person should go to city A.

So we can sort the pairs <cost[i] - cost[i], i> in ascending order. The first half goes to city A, the second half goes to city B.

// OJ: https://leetcode.com/problems/two-city-scheduling/

// Time: O(NlogN)
// Space: O(N)
class Solution {
public:
int twoCitySchedCost(vector<vector<int>>& A) {
int N = A.size(), ans = 0;
vector<pair<int, int>> v;
for (int i = 0; i < N; ++i) v.emplace_back(A[i] - A[i], i);
sort(begin(v), end(v));
for (int i = 0; i < N / 2; ++i) ans += A[v[i].second];
for (int i = N / 2; i < N; ++i) ans += A[v[i].second];
return ans;
}
};


Java

class Solution {
public int twoCitySchedCost(int[][] costs) {
int length = costs.length;
int sum = 0;
int[] differences = new int[length];
for (int i = 0; i < length; i++) {
sum += costs[i];
differences[i] = costs[i] - costs[i];
}
Arrays.sort(differences);
int halfLength = length / 2;
for (int i = 0; i < halfLength; i++)
sum += differences[i];
return sum;
}
}