A global company is organizing a massive recruiting event! They need to interview 2n candidates, but here's the challenge: they have offices in exactly two cities (let's call them City A and City B), and they need to split the candidates evenly between both locations.
You're given an array costs where costs[i] = [aCosti, bCosti] represents:
aCosti: The flight cost to send candidate i to City AbCosti: The flight cost to send candidate i to City B
Your mission: Find the minimum total cost to fly all candidates such that exactly n candidates end up in each city.
Example: If you have 4 candidates with costs [[10,20],[30,200],[400,50],[30,20]], you need to send 2 to each city. The optimal solution sends candidates 0,1 to City A and candidates 2,3 to City B for a total cost of 10+30+50+20 = 110.
Input & Output
Visualization
Time & Space Complexity
Dominated by sorting the array by cost differences
Space needed for storing the sorted array with indices
Constraints
- 2 * n == costs.length
- 2 <= costs.length <= 100
- costs.length is even
- 1 <= aCosti, bCosti <= 1000