2Brute2Best
🔥0

Two City Scheduling

Medium·LeetCode #1029 Pro OnlyBasic Reinforcement
LeetCode
01

Problem Statement

A company must fly 2n people to two cities, A and B. Person i costs costs[i][0] to fly to city A and costs[i][1] to fly to city B.

Send exactly n people to each city. Return the smallest total cost.

Input

costs: a list of [costA, costB] pairs, one per person. Its length is 2n.

Output

One integer: the smallest total cost with exactly n people in each city.

Example 1
Input: costs = [[10,20],[30,200],[400,50],[30,20]]
Output: 110

Persons 0 and 1 go to A (10 + 30). Persons 2 and 3 go to B (50 + 20). Total 110.

Example 2
Input: costs = [[259,770],[448,54],[926,667],[184,139],[840,118],[577,469]]
Output: 1859

Persons 0, 3 and 5 go to A (259 + 184 + 577 = 1020). Persons 1, 2 and 4 go to B (54 + 667 + 118 = 839). Total 1859.

Example 3
Input: costs = [[515,563],[451,713],[537,709],[343,819],[855,779],[457,60],[650,359],[631,42]]
Output: 3086

Four people go to each city, and the cheapest split costs 3086.

Constraints
  • • The number of people is even: `costs.length = 2n`.
  • • 2 ≤ costs.length ≤ 100
  • • 1 ≤ costA, costB ≤ 1000
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“2n people, exactly n in each city”
→ A balance rule. You cannot just send each person to their cheaper city.
“minimum total cost”
→ The total is a sum, so a swap changes it by a difference you can compare.
“exactly n people in each city”
→ The only real choice is which n people go to A. The rest are then fixed.
03

Approach Complexity Ladder

Brute Force — try every way to pick n people for AO(N · C(N, N/2))O(N)

Try every way to choose which n people go to A (C(2n, n) ways)

Better — DP over (people placed, people sent to A)O(N²)O(N²)

dp[i][a] = cheapest cost for the first i people with a of them sent to A

Optimal — sort by costA − costB, first half to AO(N log N)O(1)

Sort by costA − costB; the n people with the smallest difference go to A

Brute Force Intuition

Go through the people one by one. Each person goes to A or B, but only while that city still has an open seat. Try both choices at every step and keep the cheapest full plan. The number of full plans is C(2n, n), the ways to pick which n people go to A.

Recognition rule for this sub-pattern:

Sort by the right key (ratio, deadline, height); argue that swapping any two out-of-order items cannot help.
Remember this · Greedy
Trigger
Maximise count / minimise cost with a natural "best next step", sorting makes the choice obvious, reachability.
Think
Is there a choice I can always make safely without looking ahead?
Key question
Why can't the greedy choice be worse than the optimal one? (Try to break it with a small example.)
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Greedy translate into production architectures and low-level memory mechanics.