Community Problem

Two City Scheduling

Difficulty: Medium

Send exactly half the candidates to city A and half to city B at minimum total cost.

Two City Scheduling

Send exactly half the candidates to city A and half to city B at minimum total cost.

MEDIUM
Free
greedy
sorting
arrays
zarakamau

By @zarakamau

April 4, 2026

·

Updated May 18, 2026

672 views

14

4.3 (12)

I had a lead at Stripe describe this as their favorite "is the candidate going to over-engineer this with DP" filter. The DP solution is O(n^2); the greedy is one sort and a sum. The slick observation is that the cost of sending person i to city A versus city B differs by costs[i][0] - costs[i][1], and sorting by THAT delta tells you exactly which half goes where. The catalog covers Greedy basics but skipped this O(n log n) gem.

Two City Scheduling

A company is planning to interview 2n people. Given the array costs where costs[i] = [aCosti, bCosti], the cost of flying the i-th person to city A is aCosti, and the cost of flying the i-th person to city B is bCosti.

Return the minimum cost to fly every person to a city such that exactly n people arrive in each city.

Examples

Example 1:

  • Input: costs = [[10, 20], [30, 200], [400, 50], [30, 20]]
  • Output: 110
  • Explanation: Send person 0 to city A (cost 10), person 1 to city A (cost 30), person 2 to city B (cost 50), person 3 to city B (cost 20). Total 10 + 30 + 50 + 20 = 110.

Example 2:

  • Input: costs = [[259, 770], [448, 54], [926, 667], [184, 139], [840, 118], [577, 469]]
  • Output: 1859
  • Explanation: One optimal split: send the three with the smallest a - b to A, the rest to B.

Example 3:

  • Input: costs = [[515, 563], [451, 713], [537, 709], [343, 819], [855, 779], [457, 60], [650, 359], [631, 42]]
  • Output: 3086
  • Explanation: The greedy still resolves it cleanly with one sort.

Example 4:

  • Input: costs = [[1, 2], [2, 1]]
  • Output: 2
  • Explanation: Send each person to their cheaper city. Total 1 + 1 = 2.

Constraints

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

Follow-up

Why does sorting by aCost - bCost work? Sketch: the total cost when sending person i to A is aCost[i] + sum_of_other_b, and to B is bCost[i] + sum_of_other_a. The DELTA is aCost[i] - bCost[i]. Lining up these deltas, the smallest n go to A (largest savings to send them to A), the largest n go to B.

Solution

Hints

0/4
Hint 1
Hint 2
Hint 3
Hint 4
All Problems