Two City Scheduling - Problem

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 A
  • bCosti: 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

example_1.py โ€” Basic Case
$ Input: costs = [[10,20],[30,200],[400,50],[30,20]]
โ€บ Output: 110
๐Ÿ’ก Note: First person goes to city A for cost 10. Second person goes to city A for cost 30. Third person goes to city B for cost 50. Fourth person goes to city B for cost 20. Total minimum cost = 10 + 30 + 50 + 20 = 110.
example_2.py โ€” Equal Differences
$ Input: costs = [[259,770],[448,54],[926,667],[184,139],[840,118],[577,469]]
โ€บ Output: 1859
๐Ÿ’ก Note: With 6 people, we need 3 in each city. After sorting by cost differences and applying greedy assignment, the minimum total cost is 1859.
example_3.py โ€” Minimum Case
$ Input: costs = [[10,20],[30,200]]
โ€บ Output: 110
๐Ÿ’ก Note: With only 2 people, person 0 goes to city A (cost 10) and person 1 goes to city B (cost 200). Total cost = 10 + 200 = 210. Wait, this should be 10+200=210, but optimal is person 0 to A (10) and person 1 to A (30) = 40... Actually, we need equal split: person 0 to A (10), person 1 to B (200) = 210. But let's check: person 0 to B (20), person 1 to A (30) = 50. So minimum is actually 50 + 60 = 110 with person 1 to A and person 0 to A... No wait, we need exactly 1 in each city: either (A:0,B:1) costs 10+200=210 or (A:1,B:0) costs 30+20=50. So answer is 50.

Visualization

Tap to expand
Two City Scheduling: Greedy OptimizationCity ACity BPeople sorted by cost preference (A cost - B cost)Person 1A: $30, B: $200Diff: -$170Strongly prefers APerson 0A: $10, B: $20Diff: -$10Prefers APerson 3A: $30, B: $20Diff: +$10Prefers BPerson 2A: $400, B: $50Diff: +$350Strongly prefers BCity A: $30 + $10 = $40City B: $20 + $50 = $70Optimal Total Cost$40 + $70 = $110
Understanding the Visualization
1
Calculate Preference
For each person, calculate how much extra it costs to go to A vs B
2
Sort by Preference
People who strongly prefer A (negative difference) go first
3
Assign Optimally
First n people go to A, rest go to B for minimum total cost
Key Takeaway
๐ŸŽฏ Key Insight: Sort by cost difference (A_cost - B_cost) and assign greedily. People with negative differences strongly prefer city A and should go there first!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log n)

Dominated by sorting the array by cost differences

n
2n
โšก Linearithmic
Space Complexity
O(n)

Space needed for storing the sorted array with indices

n
2n
โšก Linearithmic Space

Constraints

  • 2 * n == costs.length
  • 2 <= costs.length <= 100
  • costs.length is even
  • 1 <= aCosti, bCosti <= 1000
Asked in
Google 12 Amazon 8 Meta 6 Microsoft 4
28.4K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen