A company is planning to interview 2n
people. Given the array costs
where costs[i] = [aCost<sub>i</sub>, bCost<sub>i</sub>]
, the cost of flying the i<sup>th</sup>
person to city a
is aCost<sub>i</sub>
, and the cost of flying the i<sup>th</sup>
person to city b
is bCost<sub>i</sub>
.
Return the minimum cost to fly every person to a city such that exactly n
people arrive in each city.
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
Could you write an algorithm to efficiently solve this problem?
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to the Two City Scheduling problem involves checking every possible assignment of people to cities. We explore every combination of sending people to City A or City B. Since we want the absolute best (minimum) cost, we need to check everything.
Here's how the algorithm would work step-by-step:
def two_city_scheduling_brute_force(costs):
number_of_people = len(costs)
half_people = number_of_people // 2
minimum_cost = float('inf')
def calculate_cost(assignment):
city_a_count = 0
total_cost_of_assignment = 0
for person_index in range(number_of_people):
if assignment[person_index] == 0:
city_a_count += 1
total_cost_of_assignment += costs[person_index][0]
else:
total_cost_of_assignment += costs[person_index][1]
if city_a_count == half_people:
return total_cost_of_assignment
else:
return float('inf')
def generate_assignments(index, current_assignment):
nonlocal minimum_cost
# Base case: all people have been assigned
if index == number_of_people:
cost_for_assignment = calculate_cost(current_assignment)
minimum_cost = min(minimum_cost, cost_for_assignment)
return
# Try assigning the current person to City A
generate_assignments(index + 1, current_assignment + [0])
# Try assigning the current person to City B
generate_assignments(index + 1, current_assignment + [1])
# Initiate recursive search to find minimum cost
# Starting with an empty assignment (no cities assigned)
generate_assignments(0, [])
return minimum_cost
The problem asks us to minimize the total cost of sending people to two different cities. The key insight is to first sort the people based on how much more expensive it is to send them to one city versus the other, then assign the first half to one city, and the second half to the other.
Here's how the algorithm would work step-by-step:
def two_city_scheduling(costs):
# Sort by the difference in cost to optimize assignment
costs.sort(key=lambda cost: cost[0] - cost[1])
number_of_people = len(costs)
city_a_count = number_of_people // 2
total_cost = 0
# Assign the first half to city A
for i in range(city_a_count):
total_cost += costs[i][0]
# Assign the second half to city B
for i in range(city_a_count, number_of_people):
total_cost += costs[i][1]
return total_cost
Case | How to Handle |
---|---|
Empty input cost array | Return 0 if the input array is empty, as no cost is incurred. |
Input array with odd number of costs | Throw an IllegalArgumentException, as the problem requires an even number of costs for pairing cities. |
Costs for all employees heavily skewed towards city A | The sorting logic will handle it correctly; after sorting, the first n people will be assigned to city A. |
Costs for all employees heavily skewed towards city B | The sorting logic will handle it correctly; after sorting, the first n people will be assigned to city A and the remaining to city B. |
Large input array size, potentially causing integer overflow when summing costs | Use a long data type for the total cost variable to prevent potential integer overflow. |
Costs are negative numbers | The problem doesn't explicitly disallow negative costs, and the solution (sorting based on cost difference) will still work correctly. |
All cost differences are the same (zero or otherwise) | The solution's sorting approach might lead to assigning the first n to A or other valid assignments - the problem doesn't have a single unique optimal answer. |
Costs include very large values exceeding practical financial amounts | Ensure chosen data type can hold these values without overflow, e.g., long in Java, or consider if scaling costs down would preserve the relative differences for optimal allocation. |