Two City Scheduling

Updated on 30 June, 2025
Two City Scheduling header image

Problem Statement

In the scenario described, a company is preparing to conduct interviews for 2n individuals. The company can choose between two cities, A and B, to host these interviews. Each candidate has an associated cost required to travel to each city given in a 2D array costs, where each subarray represents the costs [aCosti, bCosti]. The goal is to find the minimum expenditure required to send exactly half (n) of the candidates to each city.

Examples

Example 1

Input:

costs = [[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.

Example 2

Input:

costs = [[259,770],[448,54],[926,667],[184,139],[840,118],[577,469]]

Output:

1859

Example 3

Input:

costs = [[515,563],[451,713],[537,709],[343,819],[855,779],[457,60],[650,359],[631,42]]

Output:

3086

Constraints

  • 2 * n == costs.length
  • 2 <= costs.length <= 100
  • costs.length is even.
  • 1 <= aCosti, bCosti <= 1000

Approach and Intuition

When considering how to allocate people to the two cities to minimize total travel costs, a clear strategy involves examining the cost difference for each person for traveling between city A and city B. Here’s how you can intuitively think about approaching this problem:

  1. Calculate a "difference" list to understand which participants prefer which city in terms of cost-efficiency. This difference can be understood as bCosti - aCosti, representing how much more or less expensive it is to send someone to city B instead of city A.
  2. Sort participants primarily based on this difference. Those who have a high negative value are better off going to city A since aCosti is significantly cheaper than bCosti, and vice versa.
  3. After sorting, choose the first n people (from the sorted list) to travel to the city where they are cheaper to send, and the remaining n to the other city.
  4. Sum the appropriate costs from the costs array once the allocation is complete. The first n take their costs for the cheaper city they are sorted into, and the latter half takes the other.

By following the above plan, we distribute the interviewees in a way that ensures each city gets exactly n people, and the overall traveling cost is minimized based on the given preferences. This method cleverly utilizes sorting to make a greedy-choice decision, aiming to find the optimal allocation of interviewees to cities.

Solutions

  • C++
  • Java
cpp
class Solution {
  public:
  int minTravelCost(vector<vector<int>>& costs) {
    sort(costs.begin(), costs.end(),
            [](const vector<int> &left, const vector<int> &right) {
      return (left[0] - left[1] < right[0] - right[1]);
    });
    
    int totalCost = 0;
    int half = costs.size() / 2;
    for (int i = 0; i < half; ++i) totalCost += costs[i][0] + costs[i + half][1];
    return totalCost;
  }
};

The provided C++ code implements a solution to minimize the total travel cost for a scheduling problem, where attendees can go to two different cities and each has a distinct cost. The essence of the problem is to select the most cost-effective combination so that exactly half of the group goes to each city.

The solution utilizes several key steps:

  1. The minTravelCost function starts with sorting the input costs array. The sorting criterion is functionally determined by the difference in cost between attending each city.

  2. An anonymous comparison function is used in sort, and this function dictates that we should prioritize entries based on the cost difference between the two cities. Entries where one city is significantly cheaper compared to the other are sorted first.

  3. After sorting, the algorithm calculates totalCost by iterating through the sorted costs. For the first half of the participants, the algorithm adds the cost of sending them to City A (costs[i][0]), and for the remaining half, it adds the cost of sending them to City B (costs[i + half][1]).

  4. The outcome totalCost represents the minimum cost of arranging for exactly half of the participants to each city.

This solution ensures an optimal allocation of participants to minimize the total travel expense, taking advantage of sorting and array indexing to efficiently solve the problem. The use of lambda function for custom sorting based on individual cost differences is key in array pre-processing, and the straightforward summation afterward ensures minimal computational overhead. This approach ensures efficiency and clarity in tackling the two-city scheduling challenge.

java
class Solution {
  public int minCostToHostMeetings(int[][] costs) {
    Arrays.sort(costs, new Comparator<int[]>() {
      public int compare(int[] a, int[] b) {
        return (a[0] - a[1]) - (b[0] - b[1]);
      }
    });
    
    int minTotalCost = 0;
    int half = costs.length / 2;
    for (int i = 0; i < half; i++) minTotalCost += costs[i][0] + costs[i + half][1];
    return minTotalCost;
  }
}

The solution provided for the problem "Two City Scheduling" in Java involves calculating the minimum cost to host meetings between two cities for a given set of applicants. The approach focuses on using a greedy algorithm by sorting the costs with a custom comparator and then summing up the costs as follows:

  1. Sort the costs array using a custom comparator. The comparator prioritizes the difference in cost between holding a meeting in city A and city B ((a[0] - a[1]) - (b[0] - b[1])). This step ensures that the elements with the largest savings on choosing city A over city B come first.

  2. Initialize a variable minTotalCost to zero and calculate half the length of the costs array to manage the distribution of meetings between the two cities equally.

  3. Iterate over the sorted costs array up to halfway, accumulating the minimum cost by selecting city A for the first half and city B for the second half of sorted applicants.

By following these steps, the algorithm efficiently determines the least expensive way to allocate cities to all applicants, ensuring that each city hosts half of them. The greedy approach of sorting by potential savings ensures optimal decision-making at each step.

Comments

No comments yet.