Leetcode 1029. Two City Scheduling
Problem Explanation:
A company is planning to interview 2N people and considering sending them to two cities A and B for the interview. The cost of flying the ith person to city A is given by costs[i][0] and to city B is given by costs[i][1]. The task is to find the minimum total travel cost such that exactly N people arrive in each city.
To illustrate with an example:
Input: [[10,20],[30,200],[400,50],[30,20]] Output: 110 Here, the minimum cost is obtained by sending the first two people to city A (costs 10 + 30 = 40) and the last two people to city B (costs 50 + 20 = 70). So, the total cost is 40 + 70 = 110.
The question guarantees that costs array's length is even, so it will always be possible to send N people to each city.
Approach Explanation:
The approach revolves around calculating the savings for sending each person to city A over city B, and sorting these in decreasing order. In other words, we find out how much money we can save if we fly each person to city A instead of city B. We fly the first N people (with the maximum savings) to city A and the rest to city B.
To understand this, consider the example input [[10,20],[30,200],[400,50],[30,20]]: For each person, savings if sent to city A instead of B are: 10 (for person 1), 170 (for person 2), -350 (for person 3) and 10 (for person 4). Sort the order of savings in decreasing order, we get: [[30,200],[10,20],[30,20],[400,50]]. We choose the first N people for city A, and the rest for city B, and we calculate the total cost which comes out to be minimum.
Here are the codes for the solution:
1 2python 3# Python Solution 4class Solution: 5 def twoCitySchedCost(self, costs: List[List[int]]) -> int: 6 # First, we sort the costs array by the difference between the costs of flying to city A and city B 7 costs.sort(key = lambda x: x[0] - x[1]) 8 9 total = 0 10 n = len(costs) // 2 11 12 # The first n people goes to city A 13 for i in range(n): 14 total += costs[i][0] 15 16 # The others go to city B 17 for i in range(n, 2*n): 18 total += costs[i][1] 19 20 return total
1
2java
3// Java Solution
4import java.util.Arrays;
5import java.util.Comparator;
6class Solution {
7 public int twoCitySchedCost(int[][] costs) {
8 // Sort by a gain which company has by sending a person to city A and not to city B
9 Arrays.sort(costs, new Comparator<int[]>() {
10 @Override
11 public int compare(int[] o1, int[] o2) {
12 return o1[0] - o1[1] - (o2[0] - o2[1]);
13 }
14 });
15
16 int total = 0;
17 int n = costs.length / 2;
18 // To optimize the company expenses,
19 // send the first n persons to the city A
20 // and the others to the city B
21 for (int i = 0; i < n; ++i) total += costs[i][0] + costs[i + n][1];
22 return total;
23 }
24}
1 2javascript 3// JavaScript Solution 4var twoCitySchedCost = function(costs) { 5 // Sort the costs by the cost differences in favor of city A 6 costs.sort((a, b) => (a[0] - a[1]) - (b[0] - b[1])); 7 8 let total = 0; 9 let n = costs.length / 2; 10 // Send the first n people to city A and the other n people to city B 11 for (let i = 0; i < n; i++){ 12 total += costs[i][0] + costs[i + n][1]; 13 } 14 return total; 15};
1
2cpp
3// C++ Solution
4class Solution {
5public:
6 int twoCitySchedCost(vector<vector<int>>& costs) {
7 // Sort by a gain which company has
8 // by sending a person to city A and not to city B
9 sort(costs.begin(), costs.end(), [](const vector<int>& lhs, const vector<int>& rhs){
10 return (lhs[0]-lhs[1] < rhs[0]-rhs[1]);
11 });
12
13 int total = 0;
14 int n = costs.size() / 2;
15 // To optimize the company expenses,
16 // send the first n persons to the city A
17 // and the others to the city B
18 for(size_t i = 0; i < n; ++i) total += costs[i][0] + costs[i+n][1];
19 return total;
20 }
21};
1 2csharp 3// C# Solution 4public class Solution { 5 public int TwoCitySchedCost(int[][] costs) { 6 // Sort by a gain which company has 7 // by sending a person to city A and not to city B 8 Array.Sort(costs, (a, b) => (a[0] - a[1]) - (b[0] - b[1])); 9 10 int total = 0; 11 int n = costs.Length / 2; 12 // To optimize the company expenses, 13 // send the first n persons to the city A 14 // and the others to the city B 15 for (int i = 0; i < n; ++i) total += costs[i][0] + costs[i + n][1]; 16 return total; 17 } 18}
This approach is based on the idea of prioritizing savings, by first calculating the savings for sending each person to city A over city B and then using it to your advantage. This approach would guarantee a minimum total cost for sending the same number of people to both cities.
To extend this approach to other programming languages, the same basic principles would apply. In every language, we would first calculate the difference in price between sending each person to city A and city B. Then, we will sort this list in descending order. We send the first N people, those that will save us the most money, to city A, and the rest will go to city B.
In summary, this approach is a valuable way to optimize travel costs based on insights drawn from the cost comparison between two cities. This will not only minimize the companyโs expenses but also provide them with an optimized approach to manage the interview scheduling for candidates. With Python, JavaScript, Java, C++, and C# examples provided, it covers most of the popular programming languages, each following the same logical approach. Whether you need to apply this solution in practice or learn it for your interview preparation, understanding the theory behind an 'economy class' problem is as important as understanding its algorithmic implementation in your favorite language.
Got a question?ย Ask the Teaching Assistantย anything you don't understand.
Still not clear? Ask in the Forum, ย Discordย orย Submitย the part you don't understand to our editors.